• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

BAT File That Deletes Files Below Specific Size

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.03/day)
Location
Kentucky, USA
System Name Custom
Processor Intel i7-875K 2.9GHz (OC'd to 4.0GHz)
Motherboard MSI P55-GD80
Cooling Sidewinder Custom MCP655 / Watercool HEATKILLER® CPU Rev3.0 1156 LT / Swiftech MCR320-QP / XSPC Dual
Memory G.SKILL PiS 2000MHz 8GB 6-9-6-24
Video Card(s) EVGA GTX 470 1280MB Superclocked+ 750/1500/1750
Storage Western Digital Black x2 1TB RAID-0, Seagate Barracuda 1TB, Western Digital 500GB
Display(s) ASUS VH242H 24" 1080p
Case Cooler Master HAF 932
Audio Device(s) Creative X-FI Titnium Fatal1ty Champion Series
Power Supply Corsair Professional Series AX1200 1200W
Software Windows 7 Ultimate nVidia Edition 64bit
I am wondering how to make a bat file that can delete all files below a certain size (in this case 10kb). I have a folder on my server that has game screenshots in it (taken by Punkbuster) and need to sort out the good shots from the bad, and figure this is easier than manually deleting thousands of files.. The "bad" shots (blanks with failed renders) range in size from 4kb-8kb, so the bat file can delete anything lower than 10kb with no issues. Is this possible, and if so how do I make one? Thanks for your help.


*EDIT*
I have found a bat file that can delete specific types/sizes, but it only allows you to delete file sizes of 0kb (empty text documents and such), and replacing the "0" with "10" or any other number hasn't worked for me so far.
 
@Echo off
pushd "MY Directory"
for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
popd
 
why not just sort the folder by file size and manually delete all the tiny ones every now and then?
 
@travva : Doesn't your script delete all files in a directory that are over 1MB in size?

Shouldnt that line be .... for %%j in (*) do if %%~zj lss 10000 del %%j ... to delete files smaller than 10k ?
 
@echo off
pushd "MY Directory"
for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
popd

Thanks. That worked perfectly (only thing I had to change was the "1048576" to a "10" for 10kb, not sure why but I guess it already uses kb or something).

why not just sort the folder by file size and manually delete all the tiny ones every now and then?

That was my initial idea, but when you deal with tens of servers with hundreds or thousands of screenshots every day, it takes a while.. This can be run as a service and nobody needs to worry about it. Thanks for the help guys.
 
@Travva : Doesn't your script delete all files in a directory that are over 1MB in size?

Shouldnt that line be .... for %%j in (*) do if %%~zj lss 10000 del %%j ... to delete files smaller than 10k ?

yeah lol, my bad. op got it sorted though.
 
Glad I caught that before Ninja got back to the thread.
He would not have been a happy camper if he ran the script without testing it first and it whacked everything over 1MB. :eek:

Them's the perils of programming. Three little misplaced letters and all hell breaks loose. :laugh:
 
Can someone maybe walk through what each part of that script does for me?
 
Sure ...

@Echo off <-- don't print this line or any of the preceeding lines to the console window.
pushd "My directoy" <-- in the quoted directory
for %%j in (*) <- for every file in the directory
do
if %%~zj <-- if the size of the file
lss 10000<-- is less than 10k
del %%j <-- delete the file
popd <-- go back to original directory.
 
In case you want to do it in a C# app ...
Code:
using System.IO;

DirectoryInfo _DI = new DirectoryInfo("the directory path");
FileInfo[] _FI = _DI.GetFiles();
foreach (FileInfo _F in _FI)
{
    if (_F.Length < 10000)
    {
        try
        {
            _F.Delete();
        }
        catch (IOException)
        {
            // catch the exception and throw and error or something
            // so the loop doesn't stop.
        }
    }
}

You would probably want to do a little more error checking, but you get the drift (I hope).

BTW ... I gave myself a warning for the double post. ;)
 
Glad I caught that before Ninja got back to the thread.
He would not have been a happy camper if he ran the script without testing it first and it whacked everything over 1MB. :eek:

Them's the perils of programming. Three little misplaced letters and all hell breaks loose. :laugh:

I actually tested it before I saw your post lol. Thankfully I had the file in a folder I created specifically for the test and nothing was lost ;) Thanks for the catch though (now I know why it happened).
 
I am trying to run this batch file under Windows 8.1, and I receive the following error message:

"%%j was unexpected at this time"

Any idea why this is happening? I would appreciate any help you could offer. I have to admit I am a complete novice at batch files.

Thanks! :)
 
Back
Top