Search66

The reason the code probably did not work, is that you probably did not have all of the required UDFs available or in the same script.

Here is the code that should do what you are wanting to do. PATHS have not been verified in the code.

Using hard coded C: or other paths is not a good method. Whenever possible you should allow either set variables to get you there, or read from the Registry to get the correct paths.

This requires KiXtart v4.02 or newer.
NOTE: Make sure you test on a NON-PRODUCTION system.
I have not tested this code, and if the pathing was wrong it might
delete system critical files in the system folders.



Break On
$MyTemp=READVALUE("HKCU\Environment", "TEMP")
$SystemTemp=READVALUE("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "TEMP")
$rc=cleandirectory('$MyTemp','*.*',7)
IF EXIST("C:\TEMP\*.*")
$rc=cleandirectory('C:\TEMP','*.*',7)
ENDIF
$rc=cleandirectory('$SystemTemp','*.*',7)
$rc=cleandirectory('%SystemRoot%','*.tmp',7)



function cleandirectory($directories, $filter, optional $olderas)
Dim $rc, $timediff, $filename, $filefilter, $tempdir
Dim $filecount, $dircount

$dircount=0
$filecount=0

$olderas=val($olderas)
if $olderas<0
$olderas=0
endif

if vartype($directories)<8192
dim $tempdir[0]
$tempdir[0]=$directories
$directories=$tempdir
endif
if vartype($filter)<8192
dim $filefilter[0]
$filefilter[0]=$filter
$filter=$filefilter
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)
if instr($rc,',')
$rc=split($rc,',')
$dircount=$dircount+$rc[0]
$filecount=$filecount+$rc[1]
endif
$rc=setfileattr($filename,128)
rd $filename
if not @ERROR
$dircount=$dircount+1
endif
else
$timediff=datemath(@DATE,left(getfiletime($filename),10))
if $timediff>=$olderas
$rc=setfileattr($filename,128)
del $filename
if not @ERROR
$filecount=$filecount+1
endif
endif
endif
endif
$filename=Dir('',1)
loop
endif
next
endif
next

$cleandirectory=''+$dircount+','+$filecount
endfunction

function DateMath($ExpD1,$ExpD2)
; Author: ScriptLogic Corporation
; 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


function fullfile($path1, optional $path2)
Dim $a, $path3

$a=ubound($path1)
if $a>0
$path3=$path1[$a]
redim preserve $path1[$a-1]
$path1=fullfile($path1,$path3)
endif

if ubound($path1)=0
$path1=$path1[0]
endif
if right($path1,1)='\'
$path1=left($path1,len($path1)-1)
endif

if $path2
if left($path2,1)='\'
$path2=right($path2,len($path2)-1)
endif
$fullfile=lcase($path1+'\'+$path2)
else
$fullfile=$path1
endif

endfunction

function serialdate($ExpD)
dim $z,$h,$a,$b,$c,$y,$m,$d
if instr($ExpD,'/')
$ExpD=split($ExpD,'/')
$y=val($ExpD[0])
$m=val($ExpD[1])
$d=val($ExpD[2])
if $m<3
$m=$m+12
$y=$y-1
endif
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
else
$z=0+$ExpD+306
$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
$SerialDate=right('0000'+$y,4)+'/'+right('00'+$m,2)+'/'+right('00'+$d,2)
endif
endfunction