By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

Find Newest File in VB Exp.

MindBender77
100+
P: 234
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
Share this Question
Share on Google+
4 Replies


kadghar
Expert 100+
P: 1,295
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
Expert 100+
P: 1,295
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

Expert 5K+
P: 8,434
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
100+
P: 234
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.