It deletes all files older than x days in the specified directory and all subdirectories. Additionally, one can specify specific file filters like *.tmp.
code:
;NAME CLEANTEMPFILES
;
;ACTION Cleans up the temporary directory
;
;SYNTAX CLEANTEMPFILES(DIRECTORY,DAYS)
;
;PARAMETERS DIRECTORY
; Required string containing the directory to be cleaned
;
; DAYS
; Required integer indicating how old a file must be before it is deleted
;
;REMARKS The function will recursively delete all subdirectories and files
; Requires the UDFs FULLFILE, DATEMATH
;
;RETURNS 0 if successful, otherwise error code
;
;EXAMPLE $retcode=cleantempfiles('c:\temp',7)
;
function cleantempfiles($tempdir, $olderas)
Dim $retcode, $filter[10], $filetime, $timediff, $filename, $filefilter $filter[0]='_*.*'
$filter[1]='*.tmp'
$filter[2]='~*.*'
$filter[3]='vbe'
$filter[4]='*.exd'
$filter[5]='Acrobat Distiller 5'
$filter[6]='*.log'
$filter[7]='{*}.*'
$filter[8]='*.tmp.gif'
$filter[9]='cddb'
$filter[10]='*.dir'
if exist($tempdir)
for each $filefilter in $filter
? 'Filtering '+$tempdir+' for '+$filefilter
$filefilter=fullfile($tempdir,$filefilter)
$filename=dir($filefilter,1)
While $filename<>'' and @ERROR = 0
if $filename<>'.' and $filename<>'..'
$filename=fullfile($tempdir,$filename)
if exist($filename)
$filetime=substr(getfiletime($filename),1,10)
$timediff=datemath(@DATE,$filetime)
if $timediff>$olderas
if getfileattr($filename) & 16
$retcode=cleantempfiles($filename,$olderas)
rd $filename
? 'Deleting obsolete directory '+$filename
else
del $filename
? 'Deleting obsolete file '+$filename
endif
endif
endif
endif
$filename=Dir('',1)
loop
next
endif
$cleantempfiles=@ERROR
endfunction
;NAME FULLFILE
;
;ACTION Creates a fully qualified path
;
;SYNTAX FULLFILE(PATH1, PATH2)
;
;PARAMETERS PATH1
; Required string containing pathname
;
; PATH2
; Required string containing pathname or filename
;
;RETURNS The fully qualified path or filename
;
;REMARKS none
;
;EXAMPLE $file = fullfile('c:\windows','system32\kixtart.exe')
; $file = 'c:\windows\system32\kixtart.exe'
;
function fullfile($path1, $path2)
if substr($path1,len($path1),1)='\'
$path1=substr($path1,1,len($path1)-1)
endif
if substr($path2,1,1)='\'
$path2=substr($path2,2,len($path2)-1)
endif
$fullfile=$path1+'\'+$path2
endfunction
;NAME DATEMATH
;
;ACTION Performs mathematical computations on a date or computes days between two dates
;
;SYNTAX DATEMATH(DATE1,DATE2)
; or
; DATEMATH(DATE1,DAYS)
;
;PARAMETERS DATE1
; Required string containing date in the form of "YYYY/MM/DD"
;
; DATE2 or INTEGER
; Required string containing date in the form of "YYYY/MM/DD" or an integer (positive or negative)
;
;REMARKS Date parameters must be in the form of YYYY/MM/DD.
; Requires the UDF SERIALDATE
;
;RETURNS If two dates are passed, the number of days between them is returned as in integer.
; If a date is passed as "date1" and a number of days is passed as integer, the resulting
; date will be returned in the form of YYYY/MM/DD.
;
;EXAMPLE $retcode=datemath('2001/07/31','2001/07/01')
;
function datemath($ExpD1,$ExpD2)
; if both parameters are dates (yyyy/mm/dd), a positive
; integer indicating the days between the two dates will be returned.
; if the first is a date (yyyy/mm/dd) and the second is an integer
; (number of days), a new date will be returned.
; UDF dependencies: SerialDate, abs
select
case instr($ExpD1,'/') and instr($ExpD2,'/')
; two date passed - return daysbetween integer
$DateMath=serialdate($ExpD1)-serialdate($ExpD2)
if $DateMath<0
$DateMath=$DateMath*(-1)
endif
case instr($ExpD1,'/') and 1-instr($ExpD2,'/')
; date and a number passed - return date
$ExpD2=0+$ExpD2
$Datemath=serialdate(serialdate($ExpD1)+$ExpD2)
case 1 ; incorrect parameters passed
endselect
endfunction
;NAME SERIALDATE
;
;ACTION Convert dates to numbers (and back) for the purpose of performing date math
;
;SYNTAX SERIALDATE(DATE)
;
;PARAMETERS DATE or NUMBER
; if a date is used, it must be in the form of "YYYY/MM/DD"
; if a number is used, it must be a number previously derived from this function.
;
;REMARKS This function was developed as a core routine for the DateMath( ) function. In
; normal usage, you would most like just use the DateMath( ) function which depends
; on this function.
; Algorithms used in the development of this routine were obtained from:
; http://www.capecod.net/~pbaum/date/date0.htm
;
;RETURNS If a date is passed to this function, the function returns a number. If a number is
; passed to this function, a date "YYYY/MM/DD" is returned
;
;EXAMPLE $retcode=serialdate('2001/07/01')
;
function serialdate($ExpD)
; parameter ($ExpD) must be a date (in the form of yyyy/mm/dd)
; or an integer previously derived by this function.
; if passed a date, it returns an integer.
; If passed an integer, it returns the date.
; Integers can be used for general-purpose math computations on dates
; and then converted back to dates by calling this function again.
; Algorithms were obtained from: http://www.capecod.net/~pbaum/date/date0.htm
dim $z,$h,$a,$b,$c,$y,$m,$d
if instr($ExpD,'/') ; date passed
$y=val(substr($ExpD,1,4))
$m=val(substr($ExpD,6,2))
$d=val(substr($ExpD,9,2))
if $m<3
$m=$m+12
$y=$y-1
endif
; return an integer
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
else ; integer passed
$z=0+$ExpD+306 ; force numeric
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
if $m>12
$y=$y+1
$m=$m-12
endif
if $m<9
$m='0'+$m
endif
if $d<9
$d='0'+$d
endif
; return a string date
$SerialDate=''+$y+'/'+$m+'/'+$d
endif
endfunction