Kandy477 Posted August 12, 2009 Share Posted August 12, 2009 Is there an easy way to delete all files in a directory but one? What I'm trying to do is to perform a recursive file search for a directory containing a certain file then delete all files in that directory save for a file named "config.txt". The only way I can think of to do it is to copy config.txt into a temp folder, delete all files in the directory, then copy config.text BACK to the original directory and continue on with my script. It seems that there must be an easier way to do this but I'm simply ignorant of it. Any ideas? Link to comment Share on other sites More sharing options...
dot45 Posted August 12, 2009 Share Posted August 12, 2009 you could ouput all the files to an array or file, then trim the file you want from the list, then delete everything on the list. Tools I've Created & Shared[/url][url="http://www.autoitscript.com/forum/index.php?showtopic=97177&st=0&p=698665&hl=printer&fromsearch=1&#entry698665"]Printer Migration Tool Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted August 12, 2009 Moderators Share Posted August 12, 2009 (edited) Is there an easy way to delete all files in a directory but one? What I'm trying to do is to perform a recursive file search for a directory containing a certain file then delete all files in that directory save for a file named "config.txt". The only way I can think of to do it is to copy config.txt into a temp folder, delete all files in the directory, then copy config.text BACK to the original directory and continue on with my script. It seems that there must be an easier way to do this but I'm simply ignorant of it. Any ideas? I certainly can't think of a faster way to do it than that. If FileCopy(.., ...) Then If DirRemove(., 1) Then FileMove(...., ...., 8) EndIf Doesn't seem like much overhead to me. Edited August 12, 2009 by SmOke_N Changed second FileCopy() to FileMove() so you don't have to use FileDelete() ;) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
billthecreator Posted August 12, 2009 Share Posted August 12, 2009 (edited) #Include <File.au3> $dir = "your path here" $DirArray = _FileListToArray($dir) For $d = 1 To $DirArray[0] If $DirArray[$d] <> "config.txt" Then FileDelete($dir & "\" & $DirArray[$d]) Next Edited August 13, 2009 by billthecreator [font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap Link to comment Share on other sites More sharing options...
Kandy477 Posted August 13, 2009 Author Share Posted August 13, 2009 That is perfect! Thanks! 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