# BAT File That Deletes Files Below Specific Size



## NinjaNife (Jan 20, 2011)

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.


----------



## travva (Jan 20, 2011)

@Echo off
pushd "MY Directory"
for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
popd


----------



## Mussels (Jan 20, 2011)

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


----------



## Kreij (Jan 20, 2011)

@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 ?


----------



## NinjaNife (Jan 20, 2011)

travva said:


> @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).



Mussels said:


> 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 (Jan 20, 2011)

Kreij said:


> @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.


----------



## Kreij (Jan 20, 2011)

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. 

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


----------



## Eric_Cartman (Jan 20, 2011)

Can someone maybe walk through what each part of that script does for me?


----------



## Kreij (Jan 20, 2011)

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.


----------



## Kreij (Jan 21, 2011)

In case you want to do it in a C# app ...

```
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.


----------



## NinjaNife (Jan 21, 2011)

Kreij said:


> 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.
> 
> Them's the perils of programming. Three little misplaced letters and all hell breaks loose.



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).


----------



## Jedi D (Apr 12, 2015)

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!


----------



## Jedi D (Apr 22, 2015)

Anyone?


----------

