Sign in to follow this  
Followers 0
Ruigerock

Delete *.bak from directory & subdirectories

11 posts in this topic

Hello people,

I've searched the forum but i can't seem to find it.

In the office were i work we have a financial program that sometimes creates *.bak files but doens't delete it afterwards. One directory is created per year per client so the numbers after a few years are already in aprox. 4000 directories :whistle: .

I would like to run an autoit script each night to delete all *.bak from within those 4000 directories.

After an manual search (takes hours) and deletion (less than a hour) i've removed a couple a gigabytes of bak files.

Is there an easy way for this?

; Shows the filenames of all files in the current directory, note that "." and ".." are returned.
$search = FileFindFirstFile("*.bak")  

; 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 Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

This only shows me the *.bak from the directory it is running from.

The filefindfirstfile doesn't support subdirectories searches.

Who knows a simple solution?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Searching subdirectory's can be a very complex algorithm.

Why don't you just use the Windows search function? It allows deleting all/selected files inside the search.

Edited by Manadar

Share this post


Link to post
Share on other sites

Try this

Search ("c:", "bak");replace with your search directory and file extension required

Func Search($current,$ext)

    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

            If StringRight($current & "\" & $file,StringLen($ext)) = $ext then 
               ;MsgBox (0, "Has the file extension " & $ext, $current & "\" & $file)
               ;this is where you do what you need to do
                FileDelete ($current & "\" & $file)
            Endif
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $ext)
            
        EndIf
    WEnd
    FileClose($search)

EndFunc

Share this post


Link to post
Share on other sites

Try this

Thanks ChrisL!

I tested it on a local drive and it deleted all the *.bak file i planted there :whistle:

Right now the code is just a little to complex for me but i'll be hitting the help functions.

If someone else knows a faster script? Please do post it.

That script ran for 3.94 seconds and locally searched 184 folders.

@manadar. A search and deletion take up a lot of time, i wanted to automate it.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks ChrisL!

I tested it on a local drive and it deleted all the *.bak file i planted there :whistle:

Right now the code is just a little to complex for me but i'll be hitting the help functions.

If someone else knows a faster script? Please do post it.

That script ran for 3.94 seconds and locally searched 184 folders.

@manadar. A search and deletion take up a lot of time, i wanted to automate it.

That depends on if you want to use autoit code or just call a dos command with @comspec

Is 3.94 seconds really to slow?

Using @comspec

$Path = "Z:\"
$filter="*.bak"

Run(@ComSpec & " /C DEl " & $path & $filter & " /s/f","",@sw_hide)
Edited by ChrisL

Share this post


Link to post
Share on other sites

thanx

this script is very useful not only for deleting.

you can replace"bak" in the first line with any filename (not only extension) and the script finds your file.

very nice

j.


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hee ChrisL

I tested both scripts .. AutoIT versus old-style command prompt

Test directory was year 2003, 21gb, 105.105 files and 1169 folders.

Fight! :whistle:

Result:

Autoitscript ran in 1 minute 43 seconds with an average CPU of 40%

Cmd.exe ran in 5 minutes 25 seconds with an average CPU of 3%

The question at the moment is speed versus CPU.

I'll go for speed because i will schedule the script in the late hours before the backup starts.

As usual: thanks for the fast reply and your help

edit: the very first run had a CPU of 40% average.

All the runs after the first had a average of 10%.

Final result: Directory Caseware all years : 130Gb, 554.260 files, 8399 folders, runtime was 7 minutes 35 seconds !!

It cleaned 3.2 Gb and 2687 files.

Edited by Ruigerock

Share this post


Link to post
Share on other sites

Hee ChrisL

I tested both scripts .. AutoIT versus old-style command prompt

Test directory was year 2003, 21gb, 105.105 files and 1169 folders.

Fight! :whistle:

Result:

Autoitscript ran in 1 minute 43 seconds with an average CPU of 40%

Cmd.exe ran in 5 minutes 25 seconds with an average CPU of 3%

The question at the moment is speed versus CPU.

I'll go for speed because i will schedule the script in the late hours before the backup starts.

As usual: thanks for the fast reply and your help

edit: the very first run had a CPU of 40% average.

All the runs after the first had a average of 10%.

Final result: Directory Caseware all years : 130Gb, 554.260 files, 8399 folders, runtime was 7 minutes 35 seconds !!

It cleaned 3.2 Gb and 2687 files.

If you needed to slow it down slightly to reduce CPU then put a small sleep before the end of the while loop

You can play with it, see where I put sleep (1) even with this small sleep it reduces the CPU load quite a bit.

You can adjust the value in the sleep to get the compromise between speed and CPU load.

Chris

Search ("c:", "bak");replace with your search directory and file extension required

Func Search($current,$ext)

    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

            If StringRight($current & "\" & $file,StringLen($ext)) = $ext then 
               ;MsgBox (0, "Has the file extension " & $ext, $current & "\" & $file)
               ;this is where you do what you need to do
                FileDelete ($current & "\" & $file)
            Endif
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $ext)
            
        EndIf
        Sleep (1)
    WEnd
    FileClose($search)

EndFunc

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If you needed to slow it down slightly to reduce CPU then put a small sleep before the end of the while loop

You can play with it, see where I put sleep (1) even with this small sleep it reduces the CPU load quite a bit.

You can adjust the value in the sleep to get the compromise between speed and CPU load.............

He ChrisL, you're right.

Sleep(1) reduced the processor time to max 2%.

Usefull tip, since i've got more scripts using While= -1 loops.

Greetings

Ruigerock

Edited by Ruigerock

Share this post


Link to post
Share on other sites

Is there a way to list all the files found with a sertain extention, and then type in a path where the files can be saved?

Share this post


Link to post
Share on other sites

It would also be cool to see a progress bar or the number of files found so far.

Share this post


Link to post
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
Sign in to follow this  
Followers 0