Graywalker Posted July 23, 2008 Share Posted July 23, 2008 Making a simple script to delete all the temp files from various locations. So simple, in fact, that it doesn't work at all. muttley CODE; Program to clean up all profiles temp directories and IE history Dim $aProfiles, $iFlag, $sPath, $sFilter, $deltemp, $deltempinet, $Profile, $Progress, $pStep, $fd_deltemp, $fd_tempinet #include <File.au3> #include <Array.au3> ; Open a file to report results in $ReportFile = FileOpen("CleanupResults_" & @mon & @Mday & @YEAR & ".txt",1) $dtNow = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN FileWriteLine($ReportFile, "Starting Cleanup : " & $dtNow) FileWriteLine($ReportFile, " ") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up") $Progress=1 ProgressSet($Progress) ; Search for Profile Folders and put them into an array $sPath = "C:\Documents and Settings\" $iFlag = 2 $sFilter = "*" $aProfiles = _FileListToArray($sPath,$sFilter,$iFlag) $pStep = (100/$aProfiles[0])/3 $Progress = $Progress+$pStep ProgressSet($Progress) _ArrayDelete($aProfiles, 0) $Progress = $Progress+$pStep ProgressSet($Progress) ; Shuffle through the profiles, deleteing appropriate files For $Profile in $aProfiles $deltemp = '"' & $sPath & $Profile & '\Local Settings\Temp\*.tmp"' $deltempinet = '"' & $sPath & $Profile & '\Local Settings\Temporary Internet Files\"' FileWriteLine($ReportFile,$deltemp) FileWriteLine($ReportFile,$deltempinet) $fd_deltemp = FileDelete($deltemp) FileWriteLine($ReportFile,"Deleting temp files reports " & $fd_deltemp) $Progress = $Progress+$pStep ProgressSet($Progress) $fd_tempinet = FileDelete($deltempinet) FileWriteLine($ReportFile,"Deleting temp internet files reports " & $fd_tempinet) $Progress = $Progress+$pStep ProgressSet($Progress) Next $dtNow = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN FileWriteLine($ReportFile, "Ending Cleanup : " & $dtNow) Looks all good to me.... the output, however is : CODEStarting Cleanup : 07/23/2008 10:44 "C:\Documents and Settings\All Users\Local Settings\Temp\*.tmp" "C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\amycd\Local Settings\Temp\*.tmp" "C:\Documents and Settings\amycd\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\MYUSERNAME\Local Settings\Temp\*.tmp" "C:\Documents and Settings\MYUSERNAME\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\Default User\Local Settings\Temp\*.tmp" "C:\Documents and Settings\Default User\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\LocalService\Local Settings\Temp\*.tmp" "C:\Documents and Settings\LocalService\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\USERNAME\Local Settings\Temp\*.tmp" "C:\Documents and Settings\USERNAME\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 "C:\Documents and Settings\NetworkService\Local Settings\Temp\*.tmp" "C:\Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 Ending Cleanup : 07/23/2008 10:45 ... and nothing is deleted. I've done these variations : CODEC:\Documents and Settings\All Users\Local Settings\Temp C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files C:\Documents and Settings\All Users\Local Settings\Temp\*.* C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files\*.* "C:\Documents and Settings\All Users\Local Settings\Temp" "C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files" "C:\Documents and Settings\All Users\Local Settings\Temp\" "C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files\" "C:\Documents and Settings\All Users\Local Settings\Temp\*.tmp" "C:\Documents and Settings\All Users\Local Settings\Temporary Internet Files\" and nothing works... What do I gotta do to get those dang files deleted?? Link to comment Share on other sites More sharing options...
Graywalker Posted July 23, 2008 Author Share Posted July 23, 2008 Went back and made some changes : CODE $dtShort = FileGetShortName($deltemp) & "\*.tmp" FileWriteLine($ReportFile,"dtShort is " & $dtShort) FileWriteLine($ReportFile,$deltempinet) $fd_deltemp = FileDelete($dtShort) Still not working... CODEdtShort is C:\DOCUME~1\MYUSERNAME\LOCALS~1\Temp\*.tmp "C:\Documents and Settings\MYUSERNAME\Local Settings\Temporary Internet Files\" Deleting temp files reports 0 Deleting temp internet files reports 0 Link to comment Share on other sites More sharing options...
Bert Posted July 23, 2008 Share Posted July 23, 2008 On the line $fd_deltemp = FileDelete($deltemp) I'm wondering if the path is correct. Right before that line, put this to test it: msgbox, 0, "", $deltemp) exit what this will do is show you what the path is, then after you click ok, exit the script. You should see the problem. I bet the path is wrong. The Vollatran project  My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
crzftx Posted July 23, 2008 Share Posted July 23, 2008 The files in those directories are protected and cannot be deleted. I tried changing their security and running the file as an administrator, neither worked. I moved it onto the desktop and still could not delete it. I'm not sure how they are protected but you'll need to solve that first. Good luck. Link to comment Share on other sites More sharing options...
Bert Posted July 23, 2008 Share Posted July 23, 2008 I use ccleaner to clean that stuff. The Vollatran project  My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
crzftx Posted July 23, 2008 Share Posted July 23, 2008 I use CCleaner too. It works extremely well. Link to comment Share on other sites More sharing options...
TPlanet Posted July 23, 2008 Share Posted July 23, 2008 Hi, Try with $deltemp = ($sPath & "\" & $Profile & "\Local Settings\Temp\*.tmp") $deltempinet = ($sPath & "\" & $Profile & "\Local Settings\Temporary Internet Files\*.*") TP Link to comment Share on other sites More sharing options...
Graywalker Posted July 24, 2008 Author Share Posted July 24, 2008 On the line $fd_deltemp = FileDelete($deltemp) I'm wondering if the path is correct. Right before that line, put this to test it: msgbox, 0, "", $deltemp) exit what this will do is show you what the path is, then after you click ok, exit the script. You should see the problem. I bet the path is wrong. Yeah, did that before I posted here. All the paths are correct. The output file shows the full paths. Link to comment Share on other sites More sharing options...
Graywalker Posted July 24, 2008 Author Share Posted July 24, 2008 I use CCleaner too. It works extremely well.Our Info Security guys are... er... uhm.... enthusiastic about their job.That... and I'm doing a lot of these little things to help me get a full grasp of AutoIT. muttley Link to comment Share on other sites More sharing options...
Monamo Posted July 24, 2008 Share Posted July 24, 2008 I'm wondering if the path is correct. ... I bet the path is wrong.Aye, the path referencing for the IE temp file was the first thing that flagged for me when looking at the original post. Graywalker, regarding your IE temp files, there's actually a hidden folder/subfolder structure under the "Temporary Internet Files" directory. The easiest way I've typically used to view the actual folder structure is to use the DOS "subst" command to (pseudo-)map to that as the root folder. Doing so then lets you see the "Content.IE5" subfolder and so on. As for flushing temp files, here's a snippet I found somewhere here on the forums (credit will be posted if I can remember/locate where I found it) that I use for quick cleanup on my own system. Note: the code snippet is referencing the current user profile for cleanup, but it's a starting point you should be able to modify to factor in all profiles on the system: Dim $Temp_Dir[5] $Temp_Dir[0] = 4 $Temp_Dir[1] = @HomeDrive & "\Temp\" $Temp_Dir[2] = @UserProfileDir & "\Local Settings\Temp\" $Temp_Dir[3] = @UserProfileDir & "\Recent\" $Temp_Dir[4] = @UserProfileDir & "\Local Settings\Temporary Internet Files\Content.IE5\" For $t = 1 To $Temp_Dir[0] $fHandle = FileFindFirstFile($Temp_Dir[$t] & "*.*") If $fHandle >= 0 Then While 1 $result = FileFindNextFile($fHandle) If @error Then ExitLoop If $result <> "." And $result <> ".." Then If StringInStr(FileGetAttrib($Temp_Dir[$t] & $result), "D") Then DirRemove($Temp_Dir[$t] & $result, 1) Else FileDelete($Temp_Dir[$t] & $result) EndIf EndIf WEnd FileClose($fHandle) EndIf Next - MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup] Link to comment Share on other sites More sharing options...
Graywalker Posted July 24, 2008 Author Share Posted July 24, 2008 (edited) Aye, the path referencing for the IE temp file was the first thing that flagged for me when looking at the original post. Yeah, I saw the IE temp folder wouldn't work, but the first one to the local settings\temp directory was right on. Problem was not that the folder or files were protected, it was that the FileDelete using the *.* will error out if ONE file is 'locked.' So, in order to get around that, had to be a bit more creative... or codeative. muttley This is working beautifully and gives some nice reporting as well : CODE; Shuffle through the profiles, deleteing appropriate files For $Profile in $aProfiles ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Starting Cleanup.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) $sPath = "C:\Documents and Settings\" & $Profile & "\Local Settings\Temp\" $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temp does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temp is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else $nTempFiles = $adelFiles[0] $pStep = (75/$npfiles)/$nTempFiles FileWriteLine($ReportFile,"Attempting to delete " & $nTempFiles & " from " & $sPath) ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", "Deleting " & $nTempFiles & " from " & $Profile,-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) _ArrayDelete($adelFiles, 0) For $delFile In $adelFiles $deltemp = FileDelete($sPath & "\" & $delFile) Select Case $deltemp = "0" ;FileWriteLine($ReportFile, " Deleting " & $delFile & " was unsuccessful.") $delno = $delno+1 $Progress = $Progress+$pStep ProgressSet($Progress) Case $deltemp = "1" ;FileWriteLine($ReportFile, " Deleted " & $delFile) $delyes = $delyes+1 $Progress = $Progress+$pStep ProgressSet($Progress) EndSelect Next FileWriteLine($ReportFile, $Profile & " had " & $nTempFiles & ", Deleted " & $delyes & ", unable to delete " & $delno) $delno = 0 $delyes = 0 EndSelect Although really the "Progress bar" doesn't even get up good before the program is done at this point. Will be adding back the code to empty each profile's Content.IE5 folder. Then go for the c:\temp\ and c:\windows\temp folders too. Notice I WAS writing each deleted file to the report showing success or fail, but with over 180 or so files, that just ate up too much eye-space. LOL. output is looking like : Starting Cleanup : 07/24/2008 08:58 C:\Documents and Settings\All Users\Local Settings\Temp\ does not exist. Attempting to delete 13 from C:\Documents and Settings\MYUSERNAME\Local Settings\Temp\ MYUSERNAME had , 13 Deleted , unable to delete 13 C:\Documents and Settings\Default User\Local Settings\Temp\ is empty. C:\Documents and Settings\LocalService\Local Settings\Temp\ is empty. C:\Documents and Settings\MSJAKMOD21246\Local Settings\Temp\ does not exist. C:\Documents and Settings\NetworkService\Local Settings\Temp\ is empty. Ending Cleanup : 07/24/2008 08:58 geeze...need to tidy that up a bit... lol... Edited July 24, 2008 by Graywalker Link to comment Share on other sites More sharing options...
Graywalker Posted July 24, 2008 Author Share Posted July 24, 2008 Since I bothered everyone, I'm going to offer up my working script. This goes into each profile's temp directories ( local settings\temp, local settings\Temporary Internet Files, and local settings\Temporary Internet Files\Content.IE5\ ) and deletes files and folders that it - or you as a system administrator - can. Then, it hits the C:\temp and C:\windows\temp folders as well to clean those out. And it leaves a nice little report on everything it did in the same folder it was run from. This will be handy for those who are trying to keep museum-ready computers running or installing something on an already packed PC or Server. You do have to be an administrator on the system for it to work properly. CODE#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=Cleanup_All.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; Program By Bobby Kearan to clean up all profiles temp directories and IE history Dim $aProfiles, $iFlag, $sPath, $sFilter, $deltemp, $deltempinet, $Profile, $Progress, $pStep, $fd_deltemp, $fd_tempinet, $adelFiles, $adelFiles Dim $nTempFiles, $delFile, $npfiles, $delno, $delyes, $adelFolders, $nTempFolders, $TempPath, $deldir, $ndeldir = 0, $ydeldir = 0 #include <File.au3> #include <Array.au3> ; Open a file to report results in $ReportFile = FileOpen("CleanupResults_" & @mon & @Mday & @YEAR & ".txt",1) $dtNow = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN FileWriteLine($ReportFile, "Starting Cleanup : " & $dtNow) FileWriteLine($ReportFile, " ") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up","",-1,-1,18) $Progress=1 ProgressSet($Progress) ; Search for Profile Folders and put them into an array $sPath = "C:\Documents and Settings\" $iFlag = 2 $sFilter = "*" $aProfiles = _FileListToArray($sPath,$sFilter,$iFlag) $npfiles = $aProfiles[0] $pStep = (100/$aProfiles[0])/3 $Progress = $Progress+$pStep ProgressSet($Progress) _ArrayDelete($aProfiles, 0) $Progress = $Progress+$pStep ProgressSet($Progress) ; Shuffle through the profiles, deleteing appropriate files For $Profile in $aProfiles ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Starting Cleanup.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) ; Delete files from the profile's Temp directory $sPath = "C:\Documents and Settings\" & $Profile & "\Local Settings\Temp\" $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temp does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temp is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFiles($sPath, $pStep, $ReportFile, $Profile, $Progress, $adelFiles, $delno, $delyes) FileWriteLine($ReportFile, $Profile & " had " & $nTempFiles & ", Files Deleted " & $delyes & ", unable to delete " & $delno) $delno = 0 $delyes = 0 ; try to empty any folders in the temp directory and delete those folders. $sPath = "C:\Documents and Settings\" & $Profile & "\Local Settings\Temp\" $TempPath = $sPath $iFlag = 2 $sFilter = "*" $adelFolders = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFolders ($adelFolders, $pStep, $nTempFiles, $Profile, $ReportFile, $Progress, $sPath, $TempPath, $deldir, $ydeldir, $delno, $delyes) EndSelect FileWriteLine($ReportFile, $Profile & " had " & $nTempFolders & " subfolders, " & $delyes & " Files were deleted, unable to delete " & $delno & " files, " & $ndeldir & " subfolders deleted, " & $ydeldir & " subfolders did not get deleted.") $delno = 0 $delyes = 0 EndSelect ; Cleanup Temporary Internet Files for Firefox and IE in the Profile $sPath = "C:\Documents and Settings\" & $Profile & "\Local Settings\Temporary Internet Files\" $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFiles($sPath, $pStep, $ReportFile, $Profile, $Progress, $adelFiles, $delno, $delyes) FileWriteLine($ReportFile, $Profile & " had " & $nTempFiles & " Files, " & $delyes & " were deleted, unable to delete " & $delno) $delno = 0 $delyes = 0 ; try to empty any folders in the temp directory and delete those folders. $sPath = "C:\Documents and Settings\" & $Profile & "\Local Settings\Temporary Internet Files\" $TempPath = $sPath $iFlag = 2 $sFilter = "*" $adelFolders = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFolders ($adelFolders, $pStep, $nTempFiles, $Profile, $ReportFile, $Progress, $sPath, $TempPath, $deldir, $ydeldir, $delno, $delyes) EndSelect FileWriteLine($ReportFile, $Profile & " had " & $nTempFolders & " subfolders, " & $delyes & " Files were deleted, unable to delete " & $delno & " files, " & $ndeldir & " subfolders deleted, " & $ydeldir & " subfolders did not get deleted.") $delno = 0 $delyes = 0 EndSelect Next ProgressOff() ; Delete files and folders from the Temp Directory $sPath = "C:\Temp\" $TempPath = $sPath $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFiles($sPath, $pStep, $ReportFile, $Profile, $Progress, $adelFiles, $delno, $delyes) FileWriteLine($ReportFile, $Profile & " had " & $nTempFiles & " Files, " & $delyes & " were deleted, unable to delete " & $delno) $delno = 0 $delyes = 0 ; try to empty any folders in the temp directory and delete those folders. $iFlag = 2 $sFilter = "*" $adelFolders = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFolders ($adelFolders, $pStep, $nTempFiles, $Profile, $ReportFile, $Progress, $sPath, $TempPath, $deldir, $ydeldir, $delno, $delyes) EndSelect FileWriteLine($ReportFile, $sPath & " had " & $nTempFolders & " subfolders, " & $delyes & " Files were deleted, unable to delete " & $delno & " files, " & $ndeldir & " subfolders deleted, " & $ydeldir & " subfolders did not get deleted.") $delno = 0 $delyes = 0 EndSelect ; Delete files and folders from the Windows Temp Directory $sPath = @WindowsDir & "\Temp\" $TempPath = $sPath $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Temporary Internet Files is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFiles($sPath, $pStep, $ReportFile, $Profile, $Progress, $adelFiles, $delno, $delyes) ;FileWriteLine($ReportFile, $Profile & " had " & $nTempFiles & " Files, " & $delyes & " were deleted, unable to delete " & $delno) $delno = 0 $delyes = 0 ; try to empty any folders in the temp directory and delete those folders. $iFlag = 2 $sFilter = "*" $adelFolders = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " has no subfolders.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " has no subfolders.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFolders ($adelFolders, $pStep, $nTempFiles, $Profile, $ReportFile, $Progress, $sPath, $TempPath, $deldir, $ydeldir, $delno, $delyes) EndSelect FileWriteLine($ReportFile, $sPath & " had " & $nTempFolders & " subfolders, " & $delyes & " Files were deleted, unable to delete " & $delno & " files, " & $ndeldir & " subfolders deleted, " & $ydeldir & " subfolders did not get deleted.") $delno = 0 $delyes = 0 EndSelect $dtNow = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN FileWriteLine($ReportFile, "Ending Cleanup : " & $dtNow) FileWriteLine($ReportFile, " ") FileClose($ReportFile) Exit Func DeleteFiles ($sPath, ByRef $pStep, $ReportFile, $Profile, ByRef $Progress, ByRef $adelFiles, ByRef $delno, ByRef $delyes) If IsArray($adelFiles) Then $nTempFiles = $adelFiles[0] If IsNumber($nTempFiles) Then $pStep = (75/$npfiles)/$nTempFiles FileWriteLine($ReportFile,"Attempting to delete " & $nTempFiles & " files from " & $sPath) ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", "Deleting " & $nTempFiles & " from " & $Profile,-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) _ArrayDelete($adelFiles, 0) For $delFile In $adelFiles $deltemp = FileDelete($sPath & "\" & $delFile) Select Case $deltemp = "0" ;FileWriteLine($ReportFile, " Deleting " & $delFile & " was unsuccessful.") $delno = $delno+1 $Progress = $Progress+$pStep ProgressSet($Progress) Case $deltemp = "1" ;FileWriteLine($ReportFile, " Deleted " & $delFile) $delyes = $delyes+1 $Progress = $Progress+$pStep ProgressSet($Progress) EndSelect Next EndIf EndIf EndFunc Func DeleteFolders ($adelFolders, ByRef $pStep, $nTempFiles, $Profile, $ReportFile, $Progress, ByRef $sPath, $TempPath, $deldir, $ydeldir, ByRef $delno, ByRef $delyes) $nTempFolders = $adelFolders[0] $pStep = (75/$npfiles)/$nTempFiles ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", "Deleting " & $nTempFiles & " from " & $Profile,-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) _ArrayDelete($adelFolders, 0) For $delFolder In $adelFolders $sPath = $TempPath & $delFolder & "\" $iFlag = 1 $sFilter = "*" $adelFiles = _FileListToArray($sPath,$sFilter,$iFlag) Select Case @error = 1 FileWriteLine($ReportFile, $sPath & " does not exist.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Folder does not exist.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case @error = 4 FileWriteLine($ReportFile, $sPath & " is empty.") ProgressOn("Cleanup Junk Files", "Please wait while we clean things up", $Profile & " Folder is empty.",-1,-1,18) $Progress = $Progress+$pStep ProgressSet($Progress) Case Else DeleteFiles($sPath, $pStep, $ReportFile, $Profile, $Progress, $adelFiles, $delno, $delyes) EndSelect $deldir = DirRemove($sPath,1) Select Case $deldir = "1" $ndeldir = $ndeldir+1 Case $deldir = "0" $ydeldir = $ydeldir+1 EndSelect Next EndFunc Also made one that takes a comma separated list ( 1 to xxxx) of computer names and cleans them all the same way. It would be handy for some network admins. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now