Jump to content

Delete all files but one?


Recommended Posts

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

  • Moderators

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

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

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