Page 2 of 2 <12
Topic Options
#40061 - 2003-05-12 11:43 PM Re: Using kix to nuke Windows 2000 temp files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
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

Top
#40062 - 2003-05-12 11:56 PM Re: Using kix to nuke Windows 2000 temp files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, because we are talking about noob [Wink]

this link could do some good:
How to use UDFs
_________________________
!

download KiXnet

Top
#40063 - 2003-05-13 12:22 AM Re: Using kix to nuke Windows 2000 temp files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Yes, good link Lonkero. I should have thought of that one as well. [Frown]
Top
#40064 - 2003-05-13 02:21 AM Re: Using kix to nuke Windows 2000 temp files
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And another comment: CleanDirectory accepts arrays as inputs, thus only one call is needed if the directories and the file filters are passed as arrays. Thus
code:
$dirs[0]=$MyTemp
$dirs[1]='C:\TEMP'
$dirs[2]=$SystemTemp
$rc=cleandirectory($dirs,'*.*',7)
$rc=cleandirectory('%SystemRoot%','*.tmp',7)

would work, too.
_________________________
There are two types of vessels, submarines and targets.

Top
#40065 - 2003-05-13 01:44 PM Re: Using kix to nuke Windows 2000 temp files
search66 Offline
Fresh Scripter

Registered: 2003-05-07
Posts: 25
Loc: Baltimore, MD USA
Thanks EVERYONE for the help. Your extra effort was truly appreciated.

I'll grind through this today. Thanks again!

kudos.
_________________________
God bless Opera. God bless netjak.

Top
#40066 - 2003-05-14 03:45 PM Re: Using kix to nuke Windows 2000 temp files
ke-NT Offline
Lurker

Registered: 2002-07-11
Posts: 2
Loc: Sweden
Or just exclude that directory to roam in your system policies...
_________________________
I want to know God's thoughts, the rest are details... (A. Einstein)

Top
#40067 - 2003-05-14 03:47 PM Re: Using kix to nuke Windows 2000 temp files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
now now!

there is no simple solutions allowed on this board! [Big Grin]
_________________________
!

download KiXnet

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1088 anonymous users online.
Newest Members
JimDunn, fgarcia, Viginette, ManuvdWielNL, Sir_Barrington
17890 Registered Users

Generated in 0.054 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org