Page 1 of 1 1
Topic Options
#173574 - 2007-02-03 03:35 AM Clean Temporary Files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Based mainly on the CleanDirectory UDF by Jens, this script will clean out all the temporary folders based on permissions.

If the user has Admin permissions then it will clean out ALL TEMPORARY FILES and Folders that are older than 2 days
from ALL PROFILES and the %WINDIR%\TEMP folder as well.

If the user does not have Admin rights then it will only clean out the
files older than 2 days on their own temporary folder location.

You can change the date to anything you like. I chose 2 days as that
is a pretty good amount of time to allow temp files to remain for
most processes.

Code:
If Not @LogonMode
  Break On
Else
  Break Off
EndIf
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')
$SO=SetOption('WrapAtEOL','On')

Dim $ProfilesDirectory, $Profiles, $CleanTemp
If InGroup(@WKSTA+'\'+SidToName('S-1-5-32-544'))
  ;User has Admin rights so clean all the temporary file locations
  $ProfilesDirectory = ExpandEnvironmentVars(ReadValue('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList','ProfilesDirectory'))
  $Profiles = Dir ($ProfilesDirectory)
  While $Profiles <> '' And Len($ProfilesDirectory)>15
    If $Profiles
      If GetFileAttr($ProfilesDirectory+'\'+$Profiles+'\Local Settings\Temp') & 16
        If Len($ProfilesDirectory+'\'+$Profiles)>25
          $CleanTemp = CleanDirectory($ProfilesDirectory+'\'+$Profiles+'\Local Settings\Temp','*.*',2)
        EndIf
      EndIf
    EndIf
    $Profiles = Dir ()
  Loop
  Dim $WinDir
  $WinDir = ReadValue('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion','SystemRoot')+'\'+'TEMP'
  $CleanTemp = CleanDirectory($WinDir,'*.*',2)
Else
  ;User does not have Admin rights so only clean the users temporary files
  Dim $CurrentUserTemp
  $CurrentUserTemp = ExpandEnvironmentVars(ReadValue('HKCU\Environment','TEMP'))
  If Len($CurrentUserTemp)>15
    $CleanTemp = CleanDirectory($CurrentUserTemp,'*.*',2)
  EndIf
EndIf
;Deleting these temporary files coud easily generate an error
;so having an error is often expected at this point
Exit @ERROR

;************************************
; UDF (User Defined Functions)
; DO NOT MODIFY BELOW THIS POINT
;************************************
Function CleanDirectory($directories, $filter, optional $olderas, $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

Function FullFile($p1, optional $p2)
  Dim $p,$l
  If VarType($p1) & 8192
    $p1=Join($p1,'\')
  EndIf
  If VarType($p2) & 8192
    $p2=Join($p2,'\')
  EndIf
  $p=$p1+'\'+$p2
  $l=iIf(Right($p,2)='\\','\','')
  $p=iIf(Right($p,1)='\',Left($p,-1),$p)
  If InStr($p,'\\')
    $p=FullFile(Join(Split($p,'\\'),'\'))
  EndIf
  If Left($p,1)='\' And Left($p,2)<>'\\'
    $p='\'+$p
  EndIf
  $FullFile=$l+$p
EndFunction

Function DateMath($ExpD1,$ExpD2)
  Select
    Case InStr($ExpD1,'/') And InStr($ExpD2,'/')
      $DateMath=serialdate($ExpD1)-serialdate($ExpD2)
      If $DateMath<0
        $DateMath=$DateMath*-1
      EndIf
    Case InStr($ExpD1,'/') And 1-InStr($ExpD2,'/')
      $ExpD2=0+$ExpD2
      $Datemath=SerialDate(SerialDate($ExpD1)+$ExpD2)
    Case 1 ; incorrect parameters passed
  EndSelect
EndFunction

Function SerialDate($ExpD)
  Dim $z,$h,$a,$b,$c,$y,$m,$d
  If InStr($ExpD,'/')
    $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
    $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
    $m=Right('0'+$m,2)
    $d=Right('0'+$d,2)
    $SerialDate=''+$y+'/'+$m+'/'+$d
  EndIf
EndFunction
 


.

Top
#173918 - 2007-02-14 08:02 PM Re: Clean Temporary Files [Re: NTDOC]
mrhodes Offline
Fresh Scripter

Registered: 2006-05-24
Posts: 8
This is long awaited Thnx NTDOC!! Would be great to also clear the prefetch, if you have a full xp enviroment. while waiting i just created a .bat file that does the following once a month or as i change the reg key in my script:

del "%temp%\*.*" /s /q
del "%windir%\prefetch\*.*" /s /q
del "%windir%\temp\*.*" /s /q

since i use the batch file, do you see where this might be a problem in the future or is it just as well as using kix to run it from the script?

Top
Page 1 of 1 1


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

Who's Online
0 registered and 837 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.05 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

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