469,072 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,072 developers. It's quick & easy.

Find Newest File in VB Exp.

MindBender77
234 100+
Hello All,

I'm trying to find the newest file in a directory via the datetime created. Finally, I'm trying to use that newest timestamp to inform the user of this information in a msgbox.

I've tried using My.Computer.Filesystem.GetFileInfo and a few other snippets I've found on the web but, none quite produce the desired result.

Each file in this directory has this format:
FileName_MMddyyyy-hh.mm.ss.txt

Any Help will be greatly appreciated
Jan 10 '08 #1
4 3275
kadghar
1,295 Expert 1GB
Hello All,

I'm trying to find the newest file in a directory via the datetime created. Finally, I'm trying to use that newest timestamp to inform the user of this information in a msgbox.

I've tried using My.Computer.Filesystem.GetFileInfo and a few other snippets I've found on the web but, none quite produce the desired result.

Each file in this directory has this format:
FileName_MMddyyyy-hh.mm.ss.txt

Any Help will be greatly appreciated
well, since you saved the files with a smart name, things will be quite easy.

lets say you have a string with the file name: strFile

Expand|Select|Wrap|Line Numbers
  1. dim Dat1 as date
  2. dim Str1 as string
  3. str1 = replace(replace(mid(strFile, instr(strFile,"_") +1,17),".",":"),"-"," ")
  4. dat1=  Mid(Str1, 1, 2) & "/" & Mid(Str1, 3, 2) & "/" & Mid(Str1, 5)
you can use DIR and a date array, and then check the max of that array
or simply have Dat2 to be the first file's date and then if some file has a greater date, replace it... or... many other ways.

HTH
Jan 11 '08 #2
kadghar
1,295 Expert 1GB
oh, i forgot.

Dont give up with my.computer.filesystem.getfileinfo , give it a last chance

try this :

Expand|Select|Wrap|Line Numbers
  1. dim str1, str2, path as string
  2. dim dat1, dat2 as date
  3. path = "C:\mypath\" 'where your files are
  4. str1 = dir(path) : str2 = str1
  5. dat1 = me.computer.filesystem.getfileinfo(path & str1).creationtime
  6. do 
  7.     str1=dir(): if str1="" then exit do
  8.     dat2 = me.computer.filesystem.getfileinfo(path & str1).creationtime
  9.     if dat2 > dat1 then 
  10.         dat1 = dat2
  11.         str2 = str1
  12.     end if
  13. loop
  14. msgbox "The newest file is: " path & str2
yeah!!

HTH
Jan 11 '08 #3
Killer42
8,435 Expert 8TB
Is the "FileName_" part consistent, or does it vary between files? Also, do you 100% trust the date/time encoded in the name? If not, it would be safer to retrieve the actual create or update date/time info from the operating system. I wasn't entirely clear from the question which way you were going.
Jan 11 '08 #4
MindBender77
234 100+
These examples look like exactly what I need. Thank you all. Also, to answer some questions. I was having trouble formatting VB Date to match the file name and wasn't sure how to look at each file name in the dir to determine the OS created date.

Finally, The files in the directory are copied there using a Function from an MS Access app. So the file names are distinct and constant.

Again, Thanks for all your Help!
Jan 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Glenn Mulno | last post: by
8 posts views Thread by Michael | last post: by
2 posts views Thread by Sullivan WxPyQtKinter | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.