Jump to content

Recursive File Deletion


Go to solution Solved by mikell,

Recommended Posts

Posted (edited)

Good day,

I would appreciate any assistance with the following...

Objective: To delete any-and-all specified data within the [Session_Undo] folder

1) I have six folders, each with a [Session_Undo] folders that has data that requires removal of any data within that folder

G:\Session_Master\Session_Undo\ ;Added
G:\Session_Master\Sets\Electric_Guitar\Session_Undo\
G:\Session_Master\Sets\Emcee\Session_Undo\
G:\Session_Master\Sets\Finish\Session_Undo\
G:\Session_Master\Sets\Intro_Outro\Session_Undo\
G:\Session_Master\Sets\N_Strung_Guitar\Session_Undo\
G:\Session_Master\Sets\S_Strung_Guitar\Session_Undo\

2) The following cmd script meets with the above objective

@echo off
:: -----------------------------------------------
set _spath=G:\Session_Master
:: -----------------------------------------------
for /R %_spath% %%G IN (*.u*) do del "%%G"
:: -----------------------------------------------
exit

3) How would the above be accomplished in AutoIt?
* I have looked-and-looked and have been unable to find anything definitive.

Thank you!

The following is my attempt at non-recursive file deletion:

; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
; -----------------------------------------------
local $_spath="G:\Session_Master"
; -----------------------------------------------
FileDelete($_spath & "\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\Electric_Guitar\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\Electric_Guitar_Sub\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\Emcee\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\End_Of_Performance\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\Intro_Outro\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\Intro_Outro_Sub\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\N_Strung_Guitar\Session_Undo\*.u*")
FileDelete($_spath & "\Sets\S_Strung_Guitar\Session_Undo\*.u*")
; -----------------------------------------------

 

Edited by mr-es335
Missing line added and noted
  • mr-es335 changed the title to Recursive File Deletion
Posted (edited)

You can delete it this way:

#include <File.au3>
#include <MsgBoxConstants.au3>

If Delete_U() Then
    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Delete_U", "OK")
Else
    MsgBox($MB_ICONWARNING + $MB_TOPMOST, "Delete_U", "Error")
EndIf

Func Delete_U()
    Local $sFilePath = 'G:\Session_Master'
    Local $sMask = "*.u*"
    Local $sNameLock = "Session_Undo"
    Local $iReturn = $FLTAR_FILES
    Local $aArrayF = _FileListToArrayRec($sFilePath, $sMask, $iReturn, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
    Local $F_Error = @error, $iDr = 0
    If $F_Error < 1 And IsArray($aArrayF) Then
        For $i = 1 To $aArrayF[0]
            If StringInStr($aArrayF[$i], $sNameLock) Then
                $iDr = FileDelete($aArrayF[$i] & @CRLF)
                If $iDr > 0 Then
                    ConsoleWrite("+ [" & $i & "] File Deleted: " & $aArrayF[$i] & @CRLF)
                Else
                    ConsoleWrite("! [" & $i & "] File not Deleted: " & $aArrayF[$i] & @CRLF)
                    $F_Error += 1
                EndIf
                $iDr = 0
            EndIf
        Next
        Return SetError($F_Error, 0, $F_Error > 1 ? 0 : 1)
    Else
        ConsoleWrite("Error (" & $F_Error & ") No " & $sMask & " files found in: " & $sFilePath & @CRLF)
        Return SetError(0, 0, 1)
    EndIf

EndFunc   ;==>Delete_U

 

Edited by Trong

Regards,
 

  • Solution
Posted (edited)

I would use this

#include <File.au3>
#Include <Array.au3>

$a = _FileListToArrayRec("G:\Session_Master", "Session_Undo", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
; _ArrayDisplay($a)
For $i = 1 to $a[0]
    FileDelete($a[$i] & "\*.u*")
Next

Be careful when using FileDelete !  ;)

Edited by mikell
Posted (edited)

mikell,

WOW and Double WOW!! Very simple...and yet..functional!!! Thanks for this...very much appreciated!

So...explanation:
_FileListToArrayRec: Lists files and\or folders in specified path with optional recursion to defined level and result sorting, with the option to use a wildcard selection.
$FLTAR_FOLDERS: Return Folders only
$FLTAR_RECUR: Search in all subfolders
$FLTAR_NOSORT: Not sorted
$FLTAR_FULLPATH: Full path included

; Updated
#include <File.au3>
#Include <Array.au3>

$a = _FileListToArrayRec("G:\Session_Master", "Session_Undo", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
_ArrayDisplay($a)
For $i = 1 to $a[0]
    _ArrayDisplay($a[$i]) ; Pauses the script until exited!
    FileDelete($a[$i] & "\*.u*")
Next

You stated, "Be careful when using FileDelete!"....Got it!

How about this then?...

#include <File.au3>
#Include <Array.au3>

$a = _FileListToArrayRec("C:\World_Reserve_Bank", "All_Funds", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
For $i = 1 to $a[0]
    FileDelete($a[$i] & "\*.*")
Next

mikell...again, "Thank you!" Very much appreciated.

Edited by mr-es335
Posted
16 hours ago, mr-es335 said:
_ArrayDisplay($a[$i]) ; Pauses the script until exited!

Argh no !  $a is an array, but $a[$i] is a string  yikes.gif.a90d8c01aae4806c78d910e7bab9105c.gif
As says the help file , If the function is passed a non-array variable (...) the function returns an error and the script continues.

#Include <Array.au3>
$s = "test"
_ArrayDisplay($s) 
Msgbox(0,"", $s)

If you really want to pause the script here you must use a blocking Msgbox : Msgbox(0,"", $a[$i])
BTW

16 hours ago, mr-es335 said:
FileDelete($a[$i] & "\*.*")

you get the same result (thanks again to the help file) and delete all files in the folder $a[$i]  by using simply : FileDelete($a[$i])

Posted

mikell,

Again...thanks for the assistance...appreciated!

I have "*.lnk" files located in the root of the "Session_Master" folder. I am able to recursively delete any/all .lnk data from the various sub-folders, but not from the actual root folder itself. Been looking at the HelpFile to no avail! What am I missing here?

; -----------------------------------------------
#include <File.au3>
#Include <Array.au3>
; -----------------------------------------------
$a = _FileListToArrayRec("G:\Session_Master", "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
;_ArrayDisplay($a)
; -----------------
For $i = 1 to $a[0]
    FileDelete($a[$i] & "\*.lnk")
Next
; -----------------------------------------------

Session_Master.zip

Posted

In this case you don't need to list folders, as you know the extension of the files to be deleted you can directly list these files

; -----------------------------------------------
#include <File.au3>
#Include <Array.au3>
; -----------------------------------------------
$a = _FileListToArrayRec("G:\Session_Master", "*.lnk", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
;_ArrayDisplay($a)
; -----------------
For $i = 1 to $a[0]
    FileDelete($a[$i])
Next

:)

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
  • Recently Browsing   0 members

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