DonationCoder.com Software > Post New Requests Here
IDEA: Delete all files except two particular ones
nkormanik:
This little challenge probably just involves some correct batch code, I'm guessing. But maybe someone writing a small program would be worthwhile. Don't know.
Suppose we have a tree-full of folders. Each folder has lots of .png files of various names.
I want to delete every single .png file in the entire folder-tree (recursive?) EXCEPT for two:
"keep_this_one.png"
"save_me_as_well.png"
"keep_this_one.png" and "save_me_as_well.png" MUST be kept (i.e., leave alone), in the various folders they are located in.
All the other .png files are to be deleted.
Leave any non-png files alone.
Any help?
Thanks much.
As always, if the solution already exists, please direct me.
Nicholas Kormanik
skwire:
Here's an AHK snippet that should meet your requirements:
--- Code: Autohotkey ---; Add filenames to save here, one per line.FileNamesToSave =(keep_this_one.pngsave_me_as_well.png) ; Get folder from user.FileSelectFolder, myFolder, , 2 If ! ( ErrorLevel ) ; User did not cancel.{ Loop, % myFolder . "\*.png", 0, 1 ; Recurse through PNG files. { ; Store some values. myFilename := A_LoopFileName myFileFullPath := A_LoopFileFullPath SaveFile := False ; Iterate through filenames to save and ; check against currently parsed filename. Loop, Parse, FileNamesToSave, `n, `r { If ( myFilename = A_LoopField ) { ; Filename to save matches. ; Set as true and break out. SaveFile := True Break } } If ( SaveFile = False ) { ; Change to FileDelete to delete directly. FileRecycle, % myFileFullPath } }} ExitApp
nkormanik:
Outstanding, Skwire. I'll give it a try.
Love that you've made it flexible, so that user can put in four files, ten files, a single file, etc.
Too, user can edit to focus on, say, .jpg files instead of .png files.
And, I presume, all other files will be left alone.
nkormanik:
I think your "snippet" deserves a name. Like, "Delete Except."
Maybe compile it, and allow user to put parameters into an .ini file?
nkormanik:
Worked well, Skwire.
Two suggestions:
1. I'd prefer typing into the script the name of the folder to act upon in the deletion process, just to be absolutely sure. Would save user time as well, as opposed to the Windows navigation routine.
Example: c:\5
2. Possibly a way of giving a signal that task has been completed. "Done."
I was waiting and holding my breath.
Fortunately all went as hoped.
Thanks a bunch!
Navigation
[0] Message Index
[#] Next page
Go to full version