Jump to content

Deleting Temp Files from User Profile


Recommended Posts

Hello Gurus!

I wanted to ask how to specify the user's profile when trying to delete files in a directory.

For example:

FileDelete ("C:\Documents and Settings\userid\Local Settings\Temporary Internet Files\*.*")

What I'm trying to figure out is how to replace the 'userid' with the specific username that is logged in.

Thank you so much in advance!

Link to comment
Share on other sites

Link to comment
Share on other sites

FileChangeDir(@UserProfileDir & "\Local Settings\Temporary Internet Files\")
$sSearch = FileFindFirstFile("*.*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

FileChangeDir(@UserProfileDir & "\Local Settings\Temporary Internet Files\")
$sSearch = FileFindFirstFile("*.*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

But why? FileDelete() supports wldcards.
Link to comment
Share on other sites

But why? FileDelete() supports wldcards.

I'm aware of that but I've also had issues doing it that way.

@Yashied

I was in a hurry and didn't have time to give you details in the answer.

In a folder like the internet cache there can easily be a file that can not be deleted. When you use wildcards, the first file that won't delete causes the call to error out so it doesn't move onto the next file. If that happens with the File*Find() method it will just continue the loop and move onto the next file.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

;FileChangeDir(@UserProfileDir & "\Local Settings\Temporary Internet Files\")
FileChangeDir(RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Cache"))
$sSearch = FileFindFirstFile("*.*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

Edit: I changed the FileChangeDir() line to get the path info from the registry instead.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thank you so much Gurus!! I used:

FileChangeDir(RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Cache"))
$sSearch = FileFindFirstFile("*.*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

Is there a way that FileDelete can also delete the folders in the Temp folder as well?

Thank you so much!!

Link to comment
Share on other sites

Thank you so much Gurus!! I used:

FileChangeDir(RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Cache"))
$sSearch = FileFindFirstFile("*.*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

Is there a way that FileDelete can also delete the folders in the Temp folder as well?

Thank you so much!!

Use just "*" rather than "*.*" in the FileFindFirstFile call.
Link to comment
Share on other sites

If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @Error Then ExitLoop
        If StringInStr(FileGetAttrib($sFile), "d") Then
            DirRemove($sFile)
            ContinueLoop
        EndIf
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 years later...

Delete all files/folders inside it:

$iReturn =  _FindDelAll(@TempDir)
;~ $iReturn =  _FindDelAll("C:\Temp", 1)
If $iReturn = 1 Then
   ConsoleWrite(" Success! " & @CRLF)
ElseIf $iReturn = 0 Then
   ConsoleWrite(" Error! " & @CRLF)
Else
   ConsoleWrite(" The path is not valid! " & @CRLF)
EndIf

Func _FindDelAll($zSearchDir, $zRemoveSearchDir = 0)
; /*********************************************************************** *
; * ---:| Function : _FindDelAll
; * ---:| Author   : Anonimajn 
; ************************************************************************ *
    Local $zRSD = Number($zRemoveSearchDir)
    If Not FileExists($zSearchDir) Or Not StringInStr(FileGetAttrib($zSearchDir), "D") Then
        Return Number(-1)
    Else
        If $zRSD <> 1 Then
            If StringRight($zSearchDir, 1) <> "\" Then $zSearchDir &= "\"
            Dim $xSearch, $sFile
            $xSearch = FileFindFirstFile($zSearchDir & "*")
            If $xSearch <> -1 Then
                While 1
                    $sFile = FileFindNextFile($xSearch)
                    If @error Then ExitLoop
                    If StringInStr(FileGetAttrib($zSearchDir & $sFile), "D") Then
                        _RemoveDir($zSearchDir & $sFile)
                    Else
                        _DelFile($zSearchDir & $sFile)
                    EndIf
                WEnd
            Else
                FileClose($xSearch)
                Return Number(0)
            EndIf
            FileClose($xSearch)
            Return Number(1)
        EndIf
        If $zRSD = 1 Then 
            _RemoveDir($zSearchDir)
            If Not FileExists($zSearchDir) Then 
                Return Number(1)
            Else
                Return Number(0)
            EndIf
        EndIf
    EndIf
EndFunc    ;==>_FindDel

Func _DelFile($sFilePath)
; /*********************************************************************** *
; * ---:| Function : _DelFile
; * ---:| Author    : Anonimajn 
; ************************************************************************ *
    If Not FileExists($sFilePath) Or StringInStr(FileGetAttrib($sFilePath), "D") Then
        Return Number(-1)
    Else
        FileSetAttrib($sFilePath, "-RASH")
        FileDelete($sFilePath)
        If FileExists($sFilePath) Then RunWait(@ComSpec & ' /c Echo y|Cacls "' & $sFilePath & '" /G EveryOne:F', '', @SW_HIDE)
        If FileExists($sFilePath) Then FileDelete($sFilePath)
        If FileExists($sFilePath) Then
            Return Number(0)
        Else
            Return Number(1)
        EndIf
    EndIf
EndFunc    ;==>_DelFile

Func _RemoveDir($sFolder)
; /*********************************************************************** *
; * ---:| Function : _RemoveDir
; * ---:| Author   : Anonimajn 
; ************************************************************************ *
    If FileExists($sFolder) And StringInStr(FileGetAttrib($sFolder), "D") And StringLen($sFolder) > 3 Then
        FileSetAttrib($sFolder, "-RSH", 1)
        DirRemove($sFolder, 1)
        If FileExists($sFolder) Then RunWait(@ComSpec & ' /c RD /s /q "' & $sFolder & '"', "", @SW_HIDE)
        If FileExists($sFolder) Then RunWait(@ComSpec & ' /c Echo y|Cacls "' & $sFolder & '" /G EveryOne:F & RD /s /q "' & $sFolder & '"', '', @SW_HIDE)
        If FileExists($sFolder) Then DirRemove($sFolder, 1)
        If FileExists($sFolder) Then
            Return Number(0)
        Else
            Return Number(1)
        EndIf
    Else
        Return Number(-1)
    EndIf
EndFunc    ;==>_RemoveDir

Regards,
 

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...