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.

udfArrFileOlderThan

 Keywords: udfArrFileOlderThan FileInfoToArray ArrayRemove Array File Old Older Than Newer Than Modofoed Accessed Created Determine Check List Date Day

DirChange (DirScript ())

;--------------------------------------------------------------------------------;
;udfArrFileOlderThan : Returns an array of files older than a specified day      ;
;                                                                                ;
;--------------------------------------------------------------------------------;
;strRootDir  : Directory path that contains files                                ;
;intDaysOld  : Number of days old (90 days).                                     ;
;intDateType : LastModified=2 LastAccessed=3 LastCreated=4                       ;
;--------------------------------------------------------------------------------;
;returns     : Array of file information of files older than specified date      ;
;--------------------------------------------------------------------------------;
;                                                                                ;
;--------------------------------------------------------------------------------;

#DefineFunction udfArrFileOlderThan (strRootDir, intDaysOld, intDateType)

; Check Parameters.
If !DirExist (strRootDir)
   Pause ('Error:udfFileOlderThan', 'Invalid directory specified.')
   Exit
EndIf
If !IsNumber (intDaysOld)
   Pause ('Error:udfFileOlderThan', 'Invalid number of days specified.')
   Exit
EndIf
If intDateType < 2 || intDateType > 4
   Pause ('Error:udfFileOlderThan', 'Invalid DateType Specified')
   Exit
EndIf

; Get all file data from RootDir.
arrFileInfo = FileInfoToArray (strRootDir : '*', 1 | 2)
; Remove first row.
ArrayRemove (arrFileInfo, 0, 1)

; Now loop thru array removing all files newer files.
intI = 0
While intI < ArrInfo (arrFileInfo, 1)
   strDTLastMod = arrFileInfo[intI, intDateType]
   intDiff = TimeDiffDays (TimeYmdHms (), strDTLastMod)
   ;Remove file data from array if newer than specified days.
   If intDiff < intDaysOld
      ArrayRemove (arrFileInfo, intI, 1)
   Else
      intI = intI + 1
   EndIf
EndWhile
Return arrFileInfo
#EndFunction


; Create test files with different "last modified datetime" attributes.
strDTNow = TimeYmdHms ()
intDayDiff = 5
intDayShift = 0
For intF = 1 To 3
   strFileTest = "TestFile." : intF : ".txt"
   intSize = FilePut (strFileTest, intF)
   intDayShift = intDayShift + intDayDiff
   strDTPast = TimeSubtract (strDTNow, "0:0:" : intDayShift)
   blnResult = FileTimeSetEx (strFileTest, strDTPast, 2) ; 2 = file last modified.
Next

; Get files older than ...
intDaysOld = 7
intDateType = 2 ; 2 = file last modified.
strRootDir = '.\'
strFileResult = "Result.older." : intDaysOld : ".txt"

arrFileInfo = udfArrFileOlderThan (strRootDir, intDaysOld, intDateType)
ArrayFilePutCSV (strFileResult, arrFileInfo)
ShellExecute (strFileResult, "", "", @NORMAL, "")

:CANCEL
Exit

Article ID:   W18367
Filename:   udfArrFileOlderThan.txt
File Created: 2011:10:31:09:28:22
Last Updated: 2011:10:31:09:28:22