topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday October 7, 2024, 12:12 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA: Delete all files except two particular ones  (Read 9284 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
IDEA: Delete all files except two particular ones
« on: December 08, 2013, 07:39 PM »
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

« Last Edit: December 08, 2013, 10:07 PM by skwire, Reason: Added \"IDEA:\" to subject. »

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #1 on: December 08, 2013, 10:06 PM »
Here's an AHK snippet that should meet your requirements:

Code: Autohotkey [Select]
  1. ; Add filenames to save here, one per line.
  2. FileNamesToSave =
  3. (
  4. keep_this_one.png
  5. save_me_as_well.png
  6. )
  7.  
  8. ; Get folder from user.
  9. FileSelectFolder, myFolder, , 2
  10.  
  11. If ! ( ErrorLevel ) ; User did not cancel.
  12. {
  13.     Loop, % myFolder . "\*.png", 0, 1 ; Recurse through PNG files.
  14.     {
  15.         ; Store some values.
  16.         myFilename     := A_LoopFileName
  17.         myFileFullPath := A_LoopFileFullPath
  18.         SaveFile       := False
  19.        
  20.         ; Iterate through filenames to save and
  21.         ; check against currently parsed filename.
  22.         Loop, Parse, FileNamesToSave, `n, `r
  23.         {
  24.             If ( myFilename = A_LoopField )
  25.             {
  26.                 ; Filename to save matches.
  27.                 ; Set as true and break out.
  28.                 SaveFile := True
  29.                 Break
  30.             }
  31.         }
  32.    
  33.         If ( SaveFile = False )
  34.         {
  35.             ; Change to FileDelete to delete directly.
  36.             FileRecycle, % myFileFullPath
  37.         }
  38.     }
  39. }
  40.  

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #2 on: December 08, 2013, 11:22 PM »
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

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #3 on: December 08, 2013, 11:27 PM »
I think your "snippet" deserves a name.  Like, "Delete Except."

Maybe compile it, and allow user to put parameters into an .ini file?


nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #4 on: December 09, 2013, 08:34 PM »
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!


skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #5 on: December 10, 2013, 12:11 AM »
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.
2.  Possibly a way of giving a signal that task has been completed.  "Done."

Here you go:

Code: Autohotkey [Select]
  1. ; Add filenames to save here, one per line.
  2. FileNamesToSave =
  3. (
  4. keep_this_one.png
  5. save_me_as_well.png
  6. )
  7.  
  8. ; Folder to recurse.
  9. myFolder := "c:\5"
  10.  
  11. Loop, % myFolder . "\*.png", 0, 1 ; Recurse through PNG files.
  12. {
  13.     ; Store some values.
  14.     myFilename     := A_LoopFileName
  15.     myFileFullPath := A_LoopFileFullPath
  16.     SaveFile       := False
  17.    
  18.     ; Iterate through filenames to save and
  19.     ; check against currently parsed filename.
  20.     Loop, Parse, FileNamesToSave, `n, `r
  21.     {
  22.         If ( myFilename = A_LoopField )
  23.         {
  24.             ; Filename to save matches.
  25.             ; Set as true and break out.
  26.             SaveFile := True
  27.             Break
  28.         }
  29.     }
  30.  
  31.     If ( SaveFile = False )
  32.     {
  33.         ; Change to FileDelete to delete directly.
  34.         FileRecycle, % myFileFullPath
  35.     }
  36. }
  37.  
  38. MsgBox, Done.
  39.  

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #6 on: December 10, 2013, 04:09 AM »
Works great, Skwire.

One double-click takes care of a huge task.  Amazing.

I'd say it's good to go.

Thanks!


rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,205
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #7 on: December 10, 2013, 04:25 PM »
I think your "snippet" deserves a name.  Like, "Delete Except."
I seem to remember an ancient DOS program called "DELBUT," meaning "delete everything but these."  So would Tsahi Chitin's TDEL.  There is no new thing under the sun, and continued development of PCs leads to continual reinvention of the wheel...

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 554
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #8 on: December 10, 2013, 05:55 PM »
Thanks, rjbull.  I found and downloaded both.

After reading the help files, and knowing they are DOS, I'll take Skwire's solution.

Too, of huge importance, doesn't appear those programs are capable of "recursive" action.  Skwire's handles huge trees of folders, easy peasy.


MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #9 on: December 10, 2013, 06:05 PM »
ahk does make plumbing the depths of folders really simple. A big time saver.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: IDEA: Delete all files except two particular ones
« Reply #10 on: December 10, 2013, 06:57 PM »
ahk does make plumbing the depths of folders really simple. A big time saver.

Hell yes.  AHK makes tasks like this dead simple.