Break on
$rc = cleandirectory('YOURFOLDERGOESHERE', '*.*')
? $rc
Sleep
;=-=-=-=-=-=-=-=-=-= DO NOT MODIFY ANYTHING BELOW THIS LINE=-=-=-=-=-=-=-=-=-=
;=-=-=-=-=-=-=-=-=-=THIS IS A UDF AND IT COMES READY FOR USE=-=-=-=-=-=-=-=-=-=
;FUNCTION CleanDirectory
;
;ACTION Cleans up the temporary directory
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.42 (fixed a bug in the COUNTONLY counter resulting in all files being counted)
; 1.41 (fixed bug in the OLDERAS parameter)
; 1.4 (added COUNTONLY flag)
; 1.31 (added flag to also delete hidden and system files)
; 1.3
;
;DATE CREATED 2001/12/18
;
;DATE MODIFIED 2006/04/13
;
;KIXTART 4.20+
;
;SYNTAX CLEANDIRECTORY(DIRECTORY, FILTER [,DAYS, COUNTONLY])
;
;PARAMETERS DIRECTORY
; Required string/array containing the directory(s) to be cleaned
;
; FILTER
; Required string/array containing file filters for deletable files/folders, wildcards are supported
;
; DAYS
; Optional integer indicating how old a file/folder must be before it is deleted
;
; COUNTONLY
; Optional boolean preventing the files/folders to be deleted. UDF will only return the number of files
; that match the age threshold.
;
;REMARKS The function will recursively delete all matching files and empty subdirectories
;
;DEPENDENCIES FULLFILE() @ http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=81757
; DATEMATH() @ http://www.scriptlogic.com/kixtart/FunctionLibrary_ViewFunction.aspx?ID=DateMath
;
;RETURNS Returns comma-separated number of deleted directories and files, otherwise 0
;
;EXAMPLE $rc=cleandirectory('c:\temp','*.TMP',7)
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82117
;
Function cleandirectory($directories, $filter, optional $olderas, optional $countonly)
Dim $rc, $timediff, $filename, $filefilter, $tempdir
Dim $filecount, $dircount
$dircount = 0
$filecount = 0
$olderas = IIf(Val($olderas) > 0, Val($olderas), 0)
$countonly = IIf(Val($countonly), 1, 0)
If Not (VarType($directories) & 8192)
$directories = Split($directories, '')
EndIf
If Not (VarType($filter) & 8192)
$filter = IIf(Trim($filter), $filter, '*.*')
$filter = Split($filter, '')
EndIf
For Each $tempdir in $directories
If $tempdir <> '' And Exist($tempdir)
For Each $filefilter in $filter
If $filefilter <> ''
$filefilter = fullfile($tempdir, $filefilter)
$filename = Dir($filefilter, 1)
While $filename <> '' And @ERROR = 0
If $filename <> '.' And $filename <> '..'
$filename = fullfile($tempdir, $filename)
If GetFileAttr($filename) & 16
$rc = cleandirectory($filename, $filter, $olderas, $countonly)
If InStr($rc, ',')
$rc = Split($rc, ',')
$dircount = $dircount + $rc[0]
$filecount = $filecount + $rc[1]
EndIf
$rc = SetFileAttr($filename, 128)
If Not $countonly
RD $filename
If Not @ERROR
$dircount = $dircount + 1
EndIf
EndIf
Else
$timediff = datemath(@DATE, Left(GetFileTime($filename), 10))
If $timediff >= $olderas
If $countonly
$filecount = $filecount + 1
Else
$rc = SetFileAttr($filename, 128)
Del $filename /c /f /h
If Not @ERROR
$filecount = $filecount + 1
EndIf
EndIf
EndIf
EndIf
EndIf
$filename = Dir('', 1)
Loop
EndIf
Next
EndIf
Next
$cleandirectory = '' + $dircount + ',' + $filecount
EndFunction