Jump to content

Clean Temporary Files


WhoAmI
 Share

Recommended Posts

A while back, I was using a crappy batch files to delete some of my temporary files. I often had problems using due to strange errors and whatnot, but that was ok since it was just for me. Recently however, I started getting requests on how to clean temporary files. I obviously couldn't use my batch file since it was customized for my computer so I wrote this generic script for others with similar systems to use. Since I obtained _FileList.au3 from this forum, I thought it would only be right if I posted my code for others to possibly benifit from or even expand on (Please post any good aditions that you may have!).

Here's a basic rundown on what's going on...

Command Line Options:

/? or /v - Help/About dialog box

/d - Debug mode (lists the files/folders it processes)

/q - Quiet mode (no dialogs)

Function: Deletes temporary files that are not in use.

@WindowsDir\temp

@TempDir

@UserProfileDir\Local Settings\Temporary Internet Files

@UserProfileDir\Local Settings\Temporary Internet Files\Content.IE5

@SystemDir\config\systemprofile\Local Settings\Temporary Internet Files

@HomeDrive\swsetup

@WindowsDir\*Uninstall*$

@WindowsDir\$hf_mig$

@WindowsDir\KB*.log

FIREFOX Cache (see website for 1.xx Profile locations - currently only does the first profile found)

OS Requirements: 2000 and Higher

Tested on: Windows XP (RTM, SP1, SP2, Tablet Ed, Tablet Ed 2005)

Hope it benifits someone else out there!

-WhoAmI

WARNING: Currently set up to delete any and all Microsoft Hotfix backup files so you will not be able to uninstall windows updates after running this!

WARNING: Not responsible for anything this script does to your computer. RUN AT YOUR OWN RISK

Clean Temporary Files.au3

; ----------------------------------------------------------------------------
; Clean Temporary Files
; AutoIt Version: 3.1.0
; Author:        Jaron Horst <Jaron@sound-exp[b][/b]ressions.com>
;
; Script Function:
;   Removes Temporary Files for Windows (User and System), IE, and Firefox if
;   Firefox Directory Found.
; ----------------------------------------------------------------------------

#include <RunOnceEx.au3>
#include <_FileList.au3>
#include <Array.au3>

#NoTrayIcon

Global $Debug = 0
Global $Quiet = 0
Global $Firefox = 0
Global $TotalProgress = 0
Global $CommandLineOptions

; Only works on 2000 or better!
if (@OSTYPE <> "WIN32_NT" Or @OSVersion == "WIN_NT4") Then
    MsgBox(48, "Clean Temporary Files", "This utility will only run on Windows 2000/XP and above.", 15)
    Exit
EndIf

; Function to Empty a folder and it's subfolders
Func EmptyFolder($Path, $ProgressToCover = 100, $FileMask = "*", $FileFolder = 0)
    $Files = _FileList($Path,$FileMask,$FileFolder)
    If $Debug Then
        If $Quiet <> 1 Then
            UpdateROE ($TotalProgress)
        EndIf
        _ArrayDisplay( $Files, "Files: " & $Path)
    EndIf
    If IsArray($Files) Then
        For $i = 1 To $Files[0]
            FileDelete($Path & "\" & $Files[$i])
            DirRemove($Path & "\" & $Files[$i], 1)
            If $Quiet <> 1 Then
                UpdateROE ($TotalProgress + $i / $Files[0] * $ProgressToCover)
            EndIf
        Next
        $TotalProgress = $TotalProgress + $ProgressToCover
    ElseIf $Quiet <> 1 Then
        $TotalProgress = $TotalProgress + $ProgressToCover
        UpdateROE ($TotalProgress)
    EndIf
    If $TotalProgress >= 100 Then
        $TotalProgress = 0
    EndIf
EndFunc  ;==>EmptyFolder

; Read Command Line Options
$CommandLineOptions = "Command Line Options:           " & @CRLF & "   /? or /v - Version Information and Help" & @CRLF & "        /q - Quiet Mode" & @CRLF & "        /d - Debug Mode"
If $CmdLine[0] <> 0 Then
    For $i = 1 To $CmdLine[0]
        Select
            Case StringLower($CmdLine[$i]) == "/q"
                $Quiet = 1
            Case StringLower($CmdLine[$i]) == "/d"
                $Debug = 1
            Case (StringLower($CmdLine[$i]) == "/v" Or StringLower($CmdLine[$i]) == "/?")
                MsgBox(0, "Clean Temporary Files", "Clean Temporary Files" & @CRLF & "by Jaron Horst" & @CRLF & "v0.1" & @CRLF & @CRLF & $CommandLineOptions)
                Exit
            Case Else
                MsgBox(0, "Clean Temporary Files", "Invalid command line option: " & $CmdLine[$i] & @CRLF & @CRLF & $CommandLineOptions)
                Exit
        EndSelect
    Next
EndIf

; Look for Firefox Profile
If FileExists(@AppDataDir & "\mozilla\Firefox\Profiles") Then
    $Files = _FileList (@AppDataDir & "\mozilla\Firefox\Profiles")
    If $Files[0] > 0 Then
        If FileExists(@AppDataDir & "\mozilla\Firefox\Profiles\" & $Files[1] & "\Cache") Then
            $Firefox = @AppDataDir & "\mozilla\Firefox\Profiles\" & $Files[1] & "\Cache"
        EndIf
    EndIf
EndIf

; Make GUI if needed
If $Quiet <> 1 Then
    If IsString($Firefox) Then
        RunOnceEx (StringSplit("Windows Temp Files,User Temp Files,Temporary Internet Files,Old Installation Files,Firefox Cache", ","), "Clean Temporary Files")
    Else
        RunOnceEx (StringSplit("Windows Temp Files,User Temp Files,Temporary Internet Files,Old Installation Files", ","), "Clean Temporary Files")
    EndIf
    UpdateROE ()
EndIf

; Windows Temp
EmptyFolder(@WindowsDir & "\temp")

; User Temp
EmptyFolder(@TempDir)

; IE Temp Internet Files
EmptyFolder(@UserProfileDir & "\Local Settings\Temporary Internet Files", 45)
EmptyFolder(@UserProfileDir & "\Local Settings\Temporary Internet Files\Content.IE5", 45)
EmptyFolder(@SystemDir & "\config\systemprofile\Local Settings\Temporary Internet Files",10)

; Old Installation Files
EmptyFolder(@HomeDrive,30,"swsetup",2)
EmptyFolder(@WindowsDir,30,"$*Uninstall*$",2)
EmptyFolder(@WindowsDir,30,"$hf_mig$",2)
EmptyFolder(@WindowsDir,10,"KB*.log",1)

; Firefox Cache
If IsString($Firefox) Then
    EmptyFolder($Firefox)
EndIf

RunOnceEx.au3

; ----------------------------------------------------------------------------
; RunOnceEx
; AutoIt Version: 3.1.0
; Author:        Jaron Horst <Jaron@sound-exp[b][/b]ressions.com>
;
; Script Function:
;   RunOnceEx(List,Title[,SubTitle[,Width]])
;   UpdateROE([Percent[,Status Text]])
;
;   Notes: List[0] must be the size of the list!
;   Recommended: List = StringSplit("First Item, Second Item, Third Item",", ")
; ----------------------------------------------------------------------------
#include-once

#include <GUIConstants.au3>

Global $ROE_Progress[3]
Global $ROE_GUI[1]
Global $ROE_Arrow   

Func RunOnceEx($List, $Title, $SubTitle="", $Width=220)
    Global $ROE_GUI[$List[0] + 2]
    $ROE_GUI[0] = $List[0]
    GUICreate($Title,$Width,155 + ($List[0] * 15),100,100,$WS_VISIBLE + $WS_CLIPSIBLINGS + $WS_SYSMENU)
    GUISetFont (10 , -1 ,-1,"Times New Roman")
    If $Subtitle = "" Then
        $SubTitle = $Title
    EndIf
    $SubTitle = GUICtrlCreateLabel($SubTitle,10,10,$Width - 15,25)
    GUICtrlSetFont($SubTitle,14,1000)
    $ROE_Arrow = GUICtrlCreateLabel(Chr(187),25,38,10,15)
    GUICtrlSetFont($ROE_Arrow, 8, 2000)
    GUICtrlCreateGroup("                                ",5,84 + ($List[0] * 15), $Width - 15, 35)
    $OverallProgressLabel = GUICtrlCreateLabel("Overall Progress",14,82 + ($List[0] * 15))
    GUICtrlSetFont($OverallProgressLabel, 10, 1000)
    $ROE_Progress[0] = 1
    $ROE_Progress[1] = GUICtrlCreateProgress(10,99 + ($List[0] * 15),$Width - 25,15)
    $ROE_Progress[2] = GUICtrlCreateProgress(35,200 + ($List[0] * 15),$Width - 45,10)
    For $i = 1 to $List[0]
        $ROE_GUI[$i] = GUICtrlCreateLabel($List[$i],35,38 + (($i - 1) * 15),$Width - 40,15)
        GUICtrlSetFont($ROE_GUI[$i],9,400)
    Next
    $ROE_GUI[$List[0] + 1] = GUICtrlCreateLabel("",45,200 + ($List[0] * 15),$Width - 50,15)
    GUICtrlSetFont($ROE_GUI[$List[0] + 1],9)
EndFunc

Func UpdateROE($Percent = 100, $Status = "")
    If $ROE_Progress[0] > $ROE_GUI[0] and $Percent == 100 and $Status == "" Then
        GUIDelete()
    ElseIf $Percent == 100 and $Status == "" Then
        GUICtrlSetFont($ROE_GUI[$ROE_Progress[0]],10,1000)
        GUICtrlSetFont($ROE_GUI[$ROE_Progress[0] - 1],9,400)
        GUICtrlSetData($ROE_Progress[1],100*($ROE_Progress[0] - 1)/$ROE_GUI[0])
        GUICtrlSetPos($ROE_Arrow,25,38 + (($ROE_Progress[0] - 1) * 15))
        For $i = $ROE_Progress[0] to $ROE_GUI[0]
            GUICtrlSetPos($ROE_GUI[$i],35,38 + (($i - 1) * 15))
        Next
        GUICtrlSetPos($ROE_Progress[2],35,200 + ($ROE_GUI[0] * 15))
        GUICtrlSetPos($ROE_GUI[$ROE_GUI[0] + 1],45,200 + ($ROE_GUI[0] * 15))
        GUICtrlSetData($ROE_Progress[2],0)
        GUICtrlSetData($ROE_GUI[$ROE_GUI[0] + 1],"")
        $ROE_Progress[0] = $ROE_Progress[0] + 1
    Else
        If $Percent == "" and GUICtrlRead($ROE_Progress[2]) > 0 Then
            $Percent = GUICtrlRead($ROE_Progress[2])
        ElseIf $Percent == "" Then
            $Percent = 100
        EndIf
        If $Status == "" and GUICtrlRead($ROE_GUI[$ROE_GUI[0] + 1]) <> "" Then
            $Status = GUICtrlRead($ROE_GUI[$ROE_GUI[0] + 1])
        EndIf
        If $Status <> "" and $Percent <> 100 Then
            $Move = 24
            $MinorMove = 11
        Else
            $Move = 12
            $MinorMove = 0
        EndIf
        For $i = $ROE_Progress[0] to $ROE_GUI[0]
            GUICtrlSetPos($ROE_GUI[$i],35,38 + (($i - 1) * 15) + $Move)
        Next
        If $Status <> "" Then
            GUICtrlSetPos($ROE_GUI[$ROE_GUI[0] + 1],45,20 + ($ROE_Progress[0] * 15))
            GUICtrlSetData($ROE_GUI[$ROE_GUI[0] + 1],$Status)
        EndIf
        If $Percent <> 100 Then
            GUICtrlSetPos($ROE_Progress[2],35,24 + $MinorMove + ($ROE_Progress[0] * 15))
            GUICtrlSetData($ROE_Progress[1],100*($ROE_Progress[0] - 2)/$ROE_GUI[0] + $Percent/$ROE_GUI[0])
            GUICtrlSetData($ROE_Progress[2],$Percent)
        EndIf
    EndIf
EndFunc

_FileList.au3

;===============================================================================
;
; Description:    lists all files and folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax:          _FileList($sPath, $sFilter = "*", $iFlag = 0)

; Parameter(s):     $sPath = Path to generate filelist for
;                  $iFlag = determines weather to return file or folders or both
;                   $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details
;                       $iFlag=0(Default) Return both files and folders
;                      $iFlag=1 Return files Only
;                       $iFlag=2 Return Folders Only
;
; Requirement(s):   None
; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
;                   On Failure - Returns an empty string "" if no files are found and sets @Error on errors
;                       @Error=1 Path not found or invalid
;                       @Error=2 Invalid $sFilter
;                      @Error=3 Invalid $iFlag
;
; Author(s):        SolidSnake <MetalGearX91@Hotmail.com>
; Note(s):          The array returned is one-dimensional and is made up as follows:
;                   $array[0] = Number of Files\Folders returned
;                   $array[1] = 1st File\Folder
;                   $array[2] = 2nd File\Folder
;                   $array[3] = 3rd File\Folder
;                   $array[n] = nth File\Folder
;
;                   Special Thanks to Helge and Layer for help with the $iFlag update
;===============================================================================
Func _FileList($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then
        SetError(1)
        Return ""
    EndIf
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8)="") Then
        SetError(2)
        Return 0
    EndIf
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then
        SetError(3)
        Return ""
    EndIf
    $asFileList[0] = 0
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch=-1 then 
        SetError(0)
        Return 0
    EndIf
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If $iFlag = 1 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        EndIf
        If $iFlag = 2 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        EndIf
        If $sFile = "." Or $sFile = ".." Then ContinueLoop
        ReDim $asFileList[UBound($asFileList) + 1]
        $asFileList[0] = $asFileList[0] + 1
        $asFileList[UBound($asFileList) - 1] = $sFile
    WEnd
    FileClose($hSearch)
    SetError(0)
    If $asFileList[0] = 0 Then Return ""
    Return $asFileList
EndFunc  ;==>_FileList

(Note: I got this last function from a post on this forum - IT IS NOT MINE. I do not know who to give credit to though so if anyone does - please let me know!)

Attached: EXE copy of this program in a ZIP file.

Clean_Temporary_Files.zip

Link to comment
Share on other sites

Since I obtained _FileList.au3 from this forum, I thought it would only be right if I posted my code for others to possibly benifit from or even expand on (Please post any good aditions that you may have!).

I am sure its ok to use the scripts posted in these forums... just like you did for others to use and share or even expand upon

for your script... maybe you could take a look at mine

*XPClean Menu*

its got some great stuff... and maybe you could utilize the program for your friends or build from it onto yours...

overall nice script

8)

NEWHeader1.png

Link to comment
Share on other sites

  • 1 year later...

Bumping up this older msg; it's been at the back of my mind for a long time to do something like this. But this script shown here is beyond my abilities at this time. I need something simpler more in keeping with the old bat cleaner files I used to use in Win98SE that were so very easy to edit.

Here's what I came up with putting together a couple of pieces of code I found while searching through these forums:

;
; AutoIt v3.0
;

SoundSetWaveVolume(50)   ;  adjust volume up
SoundPlay(@ScriptDir & "\WAVS\Windows XP Recycle.wav")   ;  plays a wav file sound
SoundSetWaveVolume(15)   ;  adjust volume down

;_________________________________________________________________________________

FileRecycleEmpty()   ;  empties the Recycle Bin
;_________________________________________________________________________________

_ClipEmpty()   ; empties the clipboard

Func _ClipEmpty()
    Local $Success = 0
    DllCall('user32.dll', 'int', 'OpenClipboard', 'hwnd', 0)
    If @error = 0 Then
        DllCall('user32.dll', 'int', 'EmptyClipboard')
        If @error = 0 Then $Success = 1
        DllCall('user32.dll', 'int', 'CloseClipboard')
        If $Success Then Return 1
    EndIf
    DllCall('user32.dll', 'int', 'CloseClipboard')
    Return 0
EndFunc
;_________________________________________________________________________________


Exit
; finished
I'd just like to add some system folders and files to be emptied and/or deleted. For example, need to delete this cookies file periodically:

C:\Documents and Settings\(profile changes each job)\APPS\Firefox Portable v2.0.0.3\Data\profile\cookies.txt

so was hoping that without changing script between jobs, that if I always dump my Firefox portable into my profile's "root" folder such as where it is here, that if I write a simple line to delete this file according to a relative path such as this type of thing,

%my profile%\APPS\Firefox Portable v2.0.0.3\Data\profile\cookies.txt

that the "cookies.txt" file will be deleted when this code is run.

I'd also add the usual temp folders, such as the temporary internet folder, Firefox's cache, any Recent folders there are can be emptied, etc.

CCleaner would do the job but I can't install it per the usual lockdown procedures they do nowadays to not have employees install stuff <g>. So having a safe script would be an ideal alternative.

Is there any code we can use to cover changeable profile paths? Don't know if it's even possible in AI but hoping it is.

Thanks! :)

Link to comment
Share on other sites

Today i write a AutoIt script to clean my Temp folder and empty Recycle Bin, to put in Windows Startup, it's very simple.

#cs ----------------------------------------------------------------------------------------

 AutoIt Version: 3.2.4.9
 Name:           Temp Cleaner and Auto Empty Recycle Bin
 Author:         BrunoReX (Name in forum) / ReX (A long time nickname)

 Script Function:
    Cleans the Temporary (Temp) folder and Empty your System Hard Drive Recycle Bin.
    I use it in Windows startup.

#ce ----------------------------------------------------------------------------------------

;Hide the AutoIt tray icon
Opt("TrayIconHide", 1)

;Get your System Drive by Environment to work with almost windows versions
Global Const $SYSTEMDRIVE = EnvGet('systemdrive')
;Get your Temporary (Temp) folder by Environment to work with almost windows versions
Global Const $TEMPFOLDER = EnvGet('temp')
;Get your Command Prompt by Environment to work with almost windows versions
Global Const $COMMANDPROMPT = EnvGet('comspec')

;Run Command Prompt with Del command to force deletion of Read Only files, run silent and delete files in Subfolders too.
RunWait('"'&$COMMANDPROMPT&'"'&" /c "&"del /f /s /q "&'"'&$TEMPFOLDER&'\*.*"', $TEMPFOLDER, @SW_HIDE)
; Empty Recycle Bin of your System Drive
FileRecycleEmpty($SYSTEMDRIVE)

It works on Windows 9x versions too. :)

Link to comment
Share on other sites

That looks good. I'd like to do something similar. I've just put together a basic structure, putting English text where I don't know what the AI syntax is.

;
; AutoIt v3.0
;

SoundSetWaveVolume(50)   ;  adjust volume up
SoundPlay(@ScriptDir & "\WAVS\Windows XP Recycle.wav")   ;  plays a wav file sound
SoundSetWaveVolume(15)   ;  adjust volume down

;__________________________________________________________________________________________________

FileRecycleEmpty()   ;  empties the Recycle Bin
;__________________________________________________________________________________________________
_ClipEmpty()   ; empties the clipboard

Func _ClipEmpty()
    Local $Success = 0
    DllCall('user32.dll', 'int', 'OpenClipboard', 'hwnd', 0)
    If @error = 0 Then
        DllCall('user32.dll', 'int', 'EmptyClipboard')
        If @error = 0 Then $Success = 1
        DllCall('user32.dll', 'int', 'CloseClipboard')
        If $Success Then Return 1
    EndIf
    DllCall('user32.dll', 'int', 'CloseClipboard')
    Return 0
EndFunc
;__________________________________________________________________________________________________

; EMPTY FOLDERS:
Delete folder contents (@HomeDrive & @HomePath & "\Recent")
Delete folder contents (@HomeDrive & @HomePath & "\Application Data\Microsoft\Office\Recent")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\Temp")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\Temporary Internet Files")
Delete folder contents (@HomeDrive & @HomePath & "\Cookies")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\History")

Delete folder contents (@HomeDrive & @HomePath & "\APPS\Firefox Portable\Data\profile\cookies.txt")


Delete folder contents ("C:\Temp")
Delete folder contents ("C:\Temp\Temporary Internet Files")

;__________________________________________________________________________________________________

Exit
; finished
It's the "Empty folders:" section where I don't know exactly what to do. I couldn't find what the syntax would be for "Delete folder contents". Hopefully someone here knows what could work for that.

Secondly, don't know if I applied the @HomeDrive and @HomePath correctly, as per the example given in the AI MsgBox example above. Will this be alright? That's the type of simplicity I'm looking for to make life easier. If other folders occur to me to empty, it would be so easy. Also, no prompt would be best, just like a bat file I used to use for some of this.

Thank you very much and cheers! :)

Link to comment
Share on other sites

That looks good. I'd like to do something similar. I've just put together a basic structure, putting English text where I don't know what the AI syntax is.

... [snip]

; EMPTY FOLDERS:
Delete folder contents (@HomeDrive & @HomePath & "\Recent")
Delete folder contents (@HomeDrive & @HomePath & "\Application Data\Microsoft\Office\Recent")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\Temp")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\Temporary Internet Files")
Delete folder contents (@HomeDrive & @HomePath & "\Cookies")
Delete folder contents (@HomeDrive & @HomePath & "\Local Settings\History")

Delete folder contents (@HomeDrive & @HomePath & "\APPS\Firefox Portable\Data\profile\cookies.txt")
Delete folder contents ("C:\Temp")
Delete folder contents ("C:\Temp\Temporary Internet Files")

... [snip]
It's the "Empty folders:" section where I don't know exactly what to do. I couldn't find what the syntax would be for "Delete folder contents". Hopefully someone here knows what could work for that.

Secondly, don't know if I applied the @HomeDrive and @HomePath correctly, as per the example given in the AI MsgBox example above. Will this be alright? That's the type of simplicity I'm looking for to make life easier. If other folders occur to me to empty, it would be so easy. Also, no prompt would be best, just like a bat file I used to use for some of this.

Thank you very much and cheers! :)

I'm sorry, I only just noticed that this file is in the "Example Scripts" section. I did a search on this subject and didn't realize that when I asked my question here.

I'll re-direct this to the right group and I'll know to pay attention to which groups the messages are coming up from in a search.

This actually doesn't work as is, you see. I don't know what the syntax is for "Delete folder contents", hence this post.

Thanks and sorry! :P

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...