DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Find most recently updated file in folder or any one file name in a folder

Solved!
Go to solution

Hello, I am looking for a quick method to find the most recently updated file name, OR really just one file name in a network folder.  My folder has many files inside, so I would prefer to not use any loops, etc. 

 

My confusion starts here: I can use wildcards for some commands to get file information, such as:

 

FileExist(Directory & "*.TDM")

FileDateGet(Directory & "*.TDM", "fdModify")

 

I really just want one file name in the folder so that I can use DataFileHeaderAccess.  The most recently updated file would be best, but I'm flexible. 

0 Kudos
Message 1 of 3
(4,477 Views)
Solution
Accepted by topic author Julia Dawkins

Hi Julia,

 

By any chance is this folder declared as a Search Area?  If so, then you could use a programmatic DataFinder query to return the most recently modified data file.

 

Otherwise I would use the filesystemobject in VBScript.  I can't seem to get the Item property of the file collection (fc) to work, but it's easy enough to exit the full loop after the first run through:

 

Directory = "C:\"

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder(Directory)

Set fc = f.Files

FOR Each file In fc

  Msg = Msg & file.Path & vbCRLF

   Exit FOR

NEXT

MsgBox Msg

Also, it really shouldn't take long for the loop to find you the most recent file, even if you have lots of them in that folder:

 

Directory = "C:\"

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder(Directory)

Set fc = f.Files

FOR Each file In fc

  ModifyDate = file.DateLastModified

  IF MostRecentDate < ModifyDate THEN

    MostRecentDate = ModifyDate

    FilePath = file.Path

  END IF

NEXT

MsgBox FilePath & vbCRLF & MostRecentDate

 

Bad Turpin

Product Support Engineer
National Instruments

Message 2 of 3
(4,460 Views)
Hi Brad, Yes, my folder IS declared as a Search Area, so that will work great. Thank you! Julia
0 Kudos
Message 3 of 3
(4,457 Views)