Jump to content

File Deletion Basics


etidd
 Share

Go to solution Solved by ioa747,

Recommended Posts

The following commands aren't deleting the contents of a folder and its sub-directories:

DirRemove("E:\Internet Browser Data\Brave Data", 1)
Sleep(5000)
DirRemove("E:\Internet Browser Data\Brave Data")
Sleep(5000)

I found this script on another website, but I think it is much more simple than the following script:

$search = FileFindFirstFile("*.*")
; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error <> 0 Then ExitLoop
    If $file = "keep me" Then ContinueLoop
    If $file = "." Or $file = ".." Then ContinueLoop
    If StringInStr(FileGetAttrib(@SCRIPTDIR & "\" & $file) , "D") Then
        DirRemove($file, 1)
    Else
        FileDelete(@SCRIPTDIR & "\" & $file)
    EndIf
WEnd
FileClose($search)

The objective here is to remove the old contents of a folder and replace them with updated files. I prefer to leave the parent folder in tact without deletion.

Edited by etidd
Link to comment
Share on other sites

The foregoing snippets are deleting the whole directory but only after human intervention. As soon as I interact with the files to do what I desire them to do, the directory disappears by action of the script.

Edited by etidd
Link to comment
Share on other sites

This will delete  the contents of the "Brave Data" folder leaving the path untouched. Modified from the helpfile example.

#include <APIShellExConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPIShellEx.au3>

Local $s_Dir = "E:\Internet Browser Data\Brave Data\*.*"

; Delete to Recycle Bin
If FileExists($s_Dir) Then
    _WinAPI_ShellFileOperation($s_Dir, '', $FO_DELETE, BitOR($FOF_ALLOWUNDO, $FOF_NO_UI))

    If @error Then MsgBox(0, 'Error: ' & @error, 'Extended: ' & @extended)
EndIf

 

Link to comment
Share on other sites

_EmptyDir('yOUR dIR')  This function will help you delete all files in that folder.

#RequireAdmin
#include <File.au3>

_EmptyDir("E:\Internet Browser Data\Brave Data", 1)

Func _IsFile($sPath)
    If Not FileExists($sPath) Then Return SetError(1, 0, -1)
    If StringInStr(FileGetAttrib($sPath), 'D') <> 0 Then
        Return SetError(0, 0, 0)
    Else
        Return SetError(0, 0, 1)
    EndIf
EndFunc   ;==>_IsFile
Func _DelFile($sPath, $Fc = 0)
    If _IsFile($sPath) <> 1 Then
        Return SetError(1, 0, -1)
    Else
        FileSetAttrib($sPath, "-RASH")
        FileDelete($sPath)
        If $Fc Then
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c takeown /f "' & $sPath & '" /d y', '', @SW_HIDE)
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c Echo y|icacls "' & $sPath & '" /grant EveryOne:F /q', '', @SW_HIDE)
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c Del /f /q "' & $sPath & '"', '', @SW_HIDE)
            If FileExists($sPath) Then FileDelete($sPath)
        EndIf
        If FileExists($sPath) Then Return SetError(1, 0, 0)
    EndIf
    Return SetError(0, 0, 1)
EndFunc   ;==>_DelFile
Func _RemoveDir($sPath, $Fc = 0)
    If _IsFile($sPath) <> 0 Then
        Return SetError(1, 0, -1)
    Else
        FileSetAttrib($sPath, "-RASH", 1)
        DirRemove($sPath)
        If $Fc Then
            If FileExists($sPath) Then DirRemove($sPath, 1)
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c takeown /f "' & $sPath & '" /r /d y', '', @SW_HIDE)
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c Echo y|icacls "' & $sPath & '" /grant EveryOne:F /t /q', '', @SW_HIDE)
            If FileExists($sPath) Then RunWait(@ComSpec & ' /c RD /s /q "' & $sPath & '"', '', @SW_HIDE)
        EndIf
        If FileExists($sPath) Then Return SetError(1, 0, 0)
    EndIf
    Return SetError(0, 0, 1)
EndFunc   ;==>_RemoveDir
Func _EmptyDir($sPath, $Fc = 0)
    Local $zSearchDir = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\"
    If _IsFile($zSearchDir) <> 0 Then Return SetError(1, @error, 0)
    Local $ListOfContents = _FileListToArrayRec($zSearchDir, '*', 0, 1, 1, 2)
    If IsArray($ListOfContents) Then
        For $i = 1 To $ListOfContents[0]
            _DelIt($zSearchDir & $ListOfContents[$i], $Fc)
        Next
    EndIf
    If (DirGetSize($sPath) > 1024) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, 1)
EndFunc   ;==>_EmptyDir
Func _DelIt($sPath, $Fc = 0)
    If Not FileExists($sPath) Then Return SetError(1, 0, -1)
    If _IsFile($sPath) <> 0 Then
        Return SetError(0, 0, _DelFile($sPath, $Fc))
    Else
        Return SetError(0, 0, _RemoveDir($sPath, $Fc))
    EndIf
EndFunc   ;==>_DelIt

 

Regards,
 

Link to comment
Share on other sites

Trong,

I created a new .au3 file in the Include folder and used the _EmptyDir() function without success.

Assuming you wrote that code, it is very intriguing but beyond my level of comprehension as I have just started to learn AutoIt.

I would be interested in breaking down some of those lines with comments. I referenced the @ComSpec macro to learn what that is.

Link to comment
Share on other sites

Both benners and ioa747 supplied working examples, though I'm still deciphering this line:

_WinAPI_ShellFileOperation($s_Dir, '', $FO_DELETE, BitOR($FOF_ALLOWUNDO, $FOF_NO_UI))

Particularly, I'm learning about what a bitwise operator is.

---

As for emptying the recycle bin, I'm having an issue with the FileRecycleEmpty() function.

It does not work with these rootpaths:

FileRecycleEmpty(@HomeDrive)
FileRecycleEmpty("C:\")

It only seems to perform when it is simply FileRecycleEmpty(). Perhaps, I didn't give it long enough, though I waited for over a minute.

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