Ruigerock Posted November 2, 2006 Share Posted November 2, 2006 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 . 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? Link to comment Share on other sites More sharing options...
jvanegmond Posted November 2, 2006 Share Posted November 2, 2006 (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 November 2, 2006 by Manadar github.com/jvanegmond Link to comment Share on other sites More sharing options...
ChrisL Posted November 2, 2006 Share Posted November 2, 2006 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 [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
Ruigerock Posted November 2, 2006 Author Share Posted November 2, 2006 Try thisThanks ChrisL!I tested it on a local drive and it deleted all the *.bak file i planted there 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. Link to comment Share on other sites More sharing options...
ChrisL Posted November 2, 2006 Share Posted November 2, 2006 (edited) Thanks ChrisL! I tested it on a local drive and it deleted all the *.bak file i planted there 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 November 2, 2006 by ChrisL [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
jennico Posted November 2, 2006 Share Posted November 2, 2006 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.Don't forget this IP: 213.251.145.96 Link to comment Share on other sites More sharing options...
Ruigerock Posted November 3, 2006 Author Share Posted November 3, 2006 (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! 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 November 3, 2006 by Ruigerock Link to comment Share on other sites More sharing options...
ChrisL Posted November 3, 2006 Share Posted November 3, 2006 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! 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 [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
Ruigerock Posted November 6, 2006 Author Share Posted November 6, 2006 (edited) If you needed to slow it down slightly to reduce CPU then put a small sleep before the end of the while loopYou 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.GreetingsRuigerock Edited November 6, 2006 by Ruigerock Link to comment Share on other sites More sharing options...
Joram Posted August 27, 2007 Share Posted August 27, 2007 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? Link to comment Share on other sites More sharing options...
Joram Posted August 27, 2007 Share Posted August 27, 2007 It would also be cool to see a progress bar or the number of files found so far. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now