DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: Delete all files except two particular ones  (Read 12,594 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
IDEA: Delete all files except two particular ones
« on: December 09, 2013, 01:39 AM »
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 09, 2013, 04:07 AM by skwire »

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Delete all files except two particular ones
« Reply #1 on: December 09, 2013, 04:06 AM »
Here's an AHK snippet that should meet your requirements:

Code: (autohotkey)
; Add filenames to save here, one per line.
FileNamesToSave =
(
keep_this_one.png
save_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

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: IDEA: Delete all files except two particular ones
« Reply #2 on: December 09, 2013, 05:22 AM »
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: 563
Re: IDEA: Delete all files except two particular ones
« Reply #3 on: December 09, 2013, 05:27 AM »
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: 563
Re: IDEA: Delete all files except two particular ones
« Reply #4 on: December 10, 2013, 02:34 AM »
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

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Delete all files except two particular ones
« Reply #5 on: December 10, 2013, 06: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)
; Add filenames to save here, one per line.
FileNamesToSave =
(
keep_this_one.png
save_me_as_well.png
) 

; Folder to recurse.
myFolder := "c:\5"

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

MsgBox, Done.

ExitApp

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: IDEA: Delete all files except two particular ones
« Reply #6 on: December 10, 2013, 10: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
  • ***
  • Posts: 3,217
Re: IDEA: Delete all files except two particular ones
« Reply #7 on: December 10, 2013, 10: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: 563
Re: IDEA: Delete all files except two particular ones
« Reply #8 on: December 10, 2013, 11: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
    • Coding Snacks
Re: IDEA: Delete all files except two particular ones
« Reply #9 on: December 11, 2013, 12:05 AM »
ahk does make plumbing the depths of folders really simple. A big time saver.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Delete all files except two particular ones
« Reply #10 on: December 11, 2013, 12:57 AM »
ahk does make plumbing the depths of folders really simple. A big time saver.

Hell yes.  AHK makes tasks like this dead simple.

Original address: https://www.donationcoder.com/forum/index.php?topic=36716.0