1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

BAT File That Deletes Files Below Specific Size

Discussion in 'Programming & Webmastering' started by NinjaNife, Jan 20, 2011.

  1. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.09/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    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.
     
  2. travva

    travva New Member

    Joined:
    Aug 25, 2008
    Messages:
    349 (0.15/day)
    Thanks Received:
    57
    Location:
    Hampton, VA USA
    @Echo off
    pushd "MY Directory"
    for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
    popd
     
    NinjaNife says thanks.
  3. Mussels

    Mussels Moderprator Staff Member

    Joined:
    Oct 6, 2004
    Messages:
    42,381 (11.54/day)
    Thanks Received:
    9,684
    why not just sort the folder by file size and manually delete all the tiny ones every now and then?
     
    NinjaNife says thanks.
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    @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 ?
     
    travva and NinjaNife say thanks.
  5. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.09/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    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).

    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.
     
  6. travva

    travva New Member

    Joined:
    Aug 25, 2008
    Messages:
    349 (0.15/day)
    Thanks Received:
    57
    Location:
    Hampton, VA USA
    yeah lol, my bad. op got it sorted though.
     
  7. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    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:
     
  8. Eric_Cartman

    Eric_Cartman

    Joined:
    Mar 11, 2007
    Messages:
    587 (0.21/day)
    Thanks Received:
    45
    Can someone maybe walk through what each part of that script does for me?
     
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    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.
     
    Completely Bonkers says thanks.
  10. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    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. ;)
     
  11. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.09/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    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).
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page