ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Post New Requests Here

DONE: Delete every N files, or keep only every N files

(1/4) > >>

Deozaan:
Hi folks,

When I do Ludum Dare, I take screenshots which I can compile into a timelapse video. This means that over the course of 72 hours I often end up with around 80,000 to 120,000 screenshots. I've recently decided that I don't need to take so many screenshots and the timelapse video will be just fine, either by being shorter or having a slower framerate. But that's all kind of beside the point.

So basically, I have ~80,000+ files all in the format of ss######.png (where the #s are actually digits, such as ss000001.png) and I'd like a utility that will allow me to easily cull them down such that I either delete every N images, or perhaps even more effective, delete all of them except every N images.

Is there already a utility that exists to do this? If not, can one be provided as a Coding Snack?

Thanks.

MilesAhead:
You could run this as a test to see what would happen.  I'm paranoid about doing file deletions.  But if you have a folder with no subfolders and that folder is backed up you should be ok.

This will just say how many it would delete per setting of the Modulus in the script
Also set PngFolder to the real folder.  :)


--- ---#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
Total := 0
Deleted := 0
Modulus := 4 ; set this to every Nth file to save
Loop c:\PngFolder\*.png
{
   Total++
   if (! Mod(A_Index ,Modulus))
      continue
   Deleted++
}
MsgBox %Deleted% out of %Total% PNG files erased

skwire:
Nice work, MilesAhead.   :Thmbsup:

Here's a script that handles both scenarios.  It should be pretty self-explanatory.  Comment/uncomment the message box and delete/recycle lines as necessary.


--- Code: Autohotkey ---SetBatchLines, -1Path := "C:\tmp\04"Mode := "K" ; "Use K to keep every Nth file or D to delete every Nth file."N := 5 Loop, % Path . "\*.png"{    If ( Mode = "K" )    {        If ( Mod( A_Index, N ) != 0 )        {            MsgBox, % "Delete: " . A_LoopFileFullPath            ; FileRecycle, % A_LoopFileFullPath            ; FileDelete, % A_LoopFileFullPath        }    }    Else If ( Mode = "D" )    {        If ( Mod( A_Index, N ) = 0 )        {            MsgBox, % "Delete: " . A_LoopFileFullPath            ; FileRecycle, % A_LoopFileFullPath            ; FileDelete, % A_LoopFileFullPath        }            }}

Deozaan:
Thanks!

I'm letting it run now, keeping only every 6th file. It doesn't win any prizes for speed, but it does seem to be working as advertised. I'll let it run overnight so it can finish its thing and I'll report back later.

:Thmbsup:

MilesAhead:
Thanks!

I'm letting it run now, keeping only every 6th file. It doesn't win any prizes for speed, but it does seem to be working as advertised. I'll let it run overnight so it can finish its thing and I'll report back later.

:Thmbsup:
-Deozaan (April 22, 2015, 01:06 AM)
--- End quote ---

Even "compiled" it is run through an interpreter.  But the good thing is it is programmer coding efficient.  Meaning the file handling is so simple I could knock it off without making a mistake.  In C based I would have to use some variant of FindFirstFile FindNextFile which needs to be kept as a snippet or encapsulated in a class or wrapper once it works as desired.  Lots of little gotcha's when using the API calls directly.  :)

It might run a tad faster if compiled just because ahk would only be running the single script.  You may want to change it to output the filename it would keep to a result file to make sure it works as expected before inserting the actual delete line.

Navigation

[0] Message Index

[#] Next page

Go to full version