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

Functions

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

FileTimeCode Example code: Finds newest file

Keywords:  FileTimeCode Find newest file

Question:

I'm trying to retrieve a filename of the last saved file from fileitemize directory listing

Answer:

;***********************************************************************************
;**                       NEWEST FILE FINDER 
;** 
;**         This sample code is designed to locate   
;**         the "newest" file in a single directory.
;**
;**  NOTES: 
;**  Set PATH variable to the directory in which you would like to search
;**  Set FILETYPES variable to the wildcarded file list you would like to search
;**  Remove noted debugging lines when fully debugged.
;**
;**  Written by: Deana D.
;***********************************************************************************

;***********************************************************************************

debug(1);;;debugging only

PATH="c:\temp\" ;final backslash required
FILETYPES="*.txt|*.bak" 

dex=Direxist(PATH)
if dex==1 ;check if directory exists
	DirChange(PATH);DO NOT REMOVE
	DAFILES=FileItemize(FILETYPES);gets a list of all .txt and .bak files
	if DAFILES=="" ; if there are no files
	   message("No Files Matched","In the directory %PATH%");error message
	   exit
	endif
	count=ItemCount(DAFILES, @TAB);counts number of files in list
	For x= 1 to count	;loops the number of times that there are files
	   curfile=ItemExtract(x, DAFILES, @TAB)
	   if x==1
	      newfile=ItemExtract(x+1, DAFILES, @TAB);IMPORTANT executes only on first loop
	      if newfile=="" ;executes if only one file in the directory
	         newfile=curfile
	         message("Only one file in the directory","Newest file:%newfile% is only file");;;debugging only
	         break
	      endif
	   endif
	   a=FileTimeCode("%PATH%%curfile%")
	   b=FileTimeCode("%PATH%%newfile%")
	   if a > b
	      ans = "curfile: %curfile% is NEWER than newfile: %newfile%" ;;;;;debugging only
	      newfile=curfile;This line is very important! DO NOT REMOVE
	   else
	      ans = "curfile: %curfile% is OLDER than newfile: %newfile%" ;;;;debugging only
	      curfile="";this clears curfile variable which is useful for debugging
	   endif
	   Display(1,"Looping:%x%",ans);;;;debugging only (slows script down)
	Next
   Message("Newest file is:",newfile);Here we display the newest file
else
   Message("Opps","The Directory %PATH% is invalid");error message
endif
exit


Article ID:   W13077
Filename:   FileTimeCode Example.txt
File Created: 1999:04:15:16:51:36
Last Updated: 1999:04:15:16:51:36