WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Files and Directories

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF - Find Newest File

This UDF will locate the newest file in a directory. With a slight modification it can locate the oldest one.
#DefineFunction FindNewestFile(dir,mask)
   ;is last char of dir a \?  If not add one
   If StrSub(dir,StrLen(dir),1) !="\" Then dir=StrCat(dir,"\")
   ;tack on mask
   dirmask=StrCat(dir,mask)
   list=FileItemPath(dirmask)
   newesttime="0000:00:00:00:00:00"    ; change #1/3 to locate oldest)  0000 to 9999
   newestfile=""
   count=ItemCount(list,@TAB)

   For xx=1 To count
      thisfile=ItemExtract(xx,list,@TAB)
      thistime=FileYmdHms(thisfile)
      If thistime>newesttime    ; change (#2/3 to locate oldest)    > to < 
         newesttime=thistime
         newestfile=thisfile
      EndIf
   Next
   Return(newestfile)
                                ; change (#3/3 to locate oldest) change variables newestxxxx to oldestxxxx
#EndFunction


mydir="c:\someplace\somewhere"
DaFile=FindNewestFile(mydir,"*.*")

Message("Newest File",DaFile)

Article ID:   W16250
File Created: 2004:03:30:15:43:34
Last Updated: 2004:03:30:15:43:34