Jump to content

Search and Delete


Recommended Posts

Probably this already was asked several times but I can't find anything that work for me, sorry about my ignorance :)

I pretend to delete all files inside a directory and subdirectories except the ones with *.sav extention, I reached to this but doesn't delete inside the subdirectories.

#include <File.au3>

$sDir = 'C:\tmp\'
If @error Then Exit

$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
    If StringRight($Files[$Index],4) <> ".sav" Then
        FileDelete($sDir & "" & $Files[$Index])
    EndIf
Next

Any help is welcome.

Thanks

Link to comment
Share on other sites

 

Maybe _FileListToArrayRec is a better choice since it allows for recursion into subdirectories and the specification of inclusions & exclusions.

 

26 minutes ago, Maleficium said:

I can't find anything that work for me

I guess you didn't try looking in the Help file to see what other functions may meet your needs -- like the one listed right after _FileListToArray? ;)

Edited by TheXman
Link to comment
Share on other sites

Thanks to pointing me to the right direction TheXman, but still without success. I tried the following:

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

Example()

Func Example()

    ; A list of all files in C:\tmp\
    Local $aArray = _FileListToArrayRec("C:\tmp\", "*|*.sav", $FLTAR_FILES, $FLTAR_RECUR)
    _ArrayDisplay($aArray, "List of fliles except SAV")
EndFunc   ;==>Example

It really lists what I want to delete, now how to insert the FileDelete its a mysterious to me :)

Tryied FileDelete($aArray) everywere without success, well chinese is complicate to understand ;)

Link to comment
Share on other sites

5 hours ago, Maleficium said:

It really lists what I want to delete, now how to insert the FileDelete its a mysterious to me

Your original script had the right idea, it just needed the recursive file search. This mod works for me;-

#include <File.au3>

$sDir = "C:\tmp\"

;~ $Files = _FileListToArray($sDir,"*",1)
$Files = _FileListToArrayRec($sDir, "*|*.sav", $FLTAR_FILES, $FLTAR_RECUR)
_ArrayDisplay($Files)

For $Index = 1 To $Files[0]
  ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Files[$Index]=[' & $Files[$Index] & '] Error code: ' & @error & @CRLF)
  FileDelete($sDir & $Files[$Index])
Next

In hindsight, it doesn't now need the if test within the loop.

I have reposted the modification, looking a little neater.

Edited by pseakins
addendum, post recorrected code

Phil Seakins

Link to comment
Share on other sites

Your reply tells me that you most likely did not write the code in the original post.  If you had, then you would have known that it only required small changes to get it work with _FileListToArrayRec().

Below is an example of how it can be done:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <File.au3>
#include <Debug.au3>

_DebugSetup("File Delete Example", False, 5)
example()


Func example()
    Local $aFiles[0]

    ;Get list of files
    $aFiles = _FileListToArrayRec("C:\tmp", "*|*.sav", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "_FileListToArrayRec() failed with @error = " & @error)

    ;Process files
    For $i = 1 To $aFiles[0]
        If FileDelete($aFiles[$i]) Then
            _DebugOut(StringFormat('"%s" deleted.'         , $aFiles[$i]))
        Else
            _DebugOut(StringFormat('Unable to delete "%s".', $aFiles[$i]))
        EndIf
    Next
EndFunc

Since you are obviously new to scripting, I would suggest that you be very careful when testing & running scripts that delete files.

Edited by TheXman
Link to comment
Share on other sites

Thank you very much to both, the scripts did their work :)

Yes, I didn't wrote the first script, I was searching in the forum for hours and that was the closest that I saw to do what I want. I just did some minor changes. Like I said, almost chinese to me.

Thanks for the advice, and for the scripts.

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