Find Newest File in VB Exp.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MindBender77
    New Member
    • Jul 2007
    • 233

    Find Newest File in VB Exp.

    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.Fil esystem.GetFile Info 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_MMddyy yy-hh.mm.ss.txt

    Any Help will be greatly appreciated
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by MindBender77
    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.Fil esystem.GetFile Info 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_MMddyy yy-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

    [CODE=vb]dim Dat1 as date
    dim Str1 as string
    str1 = replace(replace (mid(strFile, instr(strFile," _") +1,17),".",":") ,"-"," ")
    dat1= Mid(Str1, 1, 2) & "/" & Mid(Str1, 3, 2) & "/" & Mid(Str1, 5)[/CODE]

    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

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      oh, i forgot.

      Dont give up with my.computer.fil esystem.getfile info , give it a last chance

      try this :

      [CODE=vb]dim str1, str2, path as string
      dim dat1, dat2 as date
      path = "C:\mypath\ " 'where your files are
      str1 = dir(path) : str2 = str1
      dat1 = me.computer.fil esystem.getfile info(path & str1).creationt ime
      do
      str1=dir(): if str1="" then exit do
      dat2 = me.computer.fil esystem.getfile info(path & str1).creationt ime
      if dat2 > dat1 then
      dat1 = dat2
      str2 = str1
      end if
      loop
      msgbox "The newest file is: " path & str2[/CODE]

      yeah!!

      HTH

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        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.

        Comment

        • MindBender77
          New Member
          • Jul 2007
          • 233

          #5
          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!

          Comment

          Working...