• 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

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.
 
Joined
Aug 25, 2008
Messages
367 (0.06/day)
Location
Hampton, VA USA
System Name pubg machine
Processor Ryzen 3950x
Motherboard MSI Gaming Edge Wifi x570
Cooling Kraken Z73
Memory 32GB Dominator RGB
Video Card(s) RTX 3080FE
Storage Firecuda 520 2TB
Display(s) Odyssey G7
Case h710i
Audio Device(s) Arctis pro wireless
Power Supply NZXT c1000
Mouse Logitech g pro wireless
Keyboard Logitech g915 tkl
Software Windows 10 x64 Pro
Benchmark Scores pwning n00bs like you
@Echo off
pushd "MY Directory"
for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
popd
 

Mussels

Freshwater Moderator
Staff member
Joined
Oct 6, 2004
Messages
58,413 (8.21/day)
Location
Oystralia
System Name Rainbow Sparkles (Power efficient, <350W gaming load)
Processor Ryzen R7 5800x3D (Undervolted, 4.45GHz all core)
Motherboard Asus x570-F (BIOS Modded)
Cooling Alphacool Apex UV - Alphacool Eisblock XPX Aurora + EK Quantum ARGB 3090 w/ active backplate
Memory 2x32GB DDR4 3600 Corsair Vengeance RGB @3866 C18-22-22-22-42 TRFC704 (1.4V Hynix MJR - SoC 1.15V)
Video Card(s) Galax RTX 3090 SG 24GB: Underclocked to 1700Mhz 0.750v (375W down to 250W))
Storage 2TB WD SN850 NVME + 1TB Sasmsung 970 Pro NVME + 1TB Intel 6000P NVME USB 3.2
Display(s) Phillips 32 32M1N5800A (4k144), LG 32" (4K60) | Gigabyte G32QC (2k165) | Phillips 328m6fjrmb (2K144)
Case Fractal Design R6
Audio Device(s) Logitech G560 | Corsair Void pro RGB |Blue Yeti mic
Power Supply Fractal Ion+ 2 860W (Platinum) (This thing is God-tier. Silent and TINY)
Mouse Logitech G Pro wireless + Steelseries Prisma XL
Keyboard Razer Huntsman TE ( Sexy white keycaps)
VR HMD Oculus Rift S + Quest 2
Software Windows 11 pro x64 (Yes, it's genuinely a good OS) OpenRGB - ditch the branded bloatware!
Benchmark Scores Nyooom.
why not just sort the folder by file size and manually delete all the tiny ones every now and then?
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
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 ?
 

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
@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.
 
Joined
Aug 25, 2008
Messages
367 (0.06/day)
Location
Hampton, VA USA
System Name pubg machine
Processor Ryzen 3950x
Motherboard MSI Gaming Edge Wifi x570
Cooling Kraken Z73
Memory 32GB Dominator RGB
Video Card(s) RTX 3080FE
Storage Firecuda 520 2TB
Display(s) Odyssey G7
Case h710i
Audio Device(s) Arctis pro wireless
Power Supply NZXT c1000
Mouse Logitech g pro wireless
Keyboard Logitech g915 tkl
Software Windows 10 x64 Pro
Benchmark Scores pwning n00bs like you
@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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
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:
 
Joined
Mar 11, 2007
Messages
702 (0.11/day)
Processor Intel Core i5 4690K
Motherboard AsRock Z97 Extreme4
Cooling Hyper 212 Evo
Memory 16GB
Video Card(s) R9 Nano
Storage 256GB SATA SSD 2TB WD Blue
Display(s) 1920x1080
Case Cooler Master Elite 130
Power Supply CX650M
Software Argh, Windows 10. I hated Windows 7. I hate Windows 10 more. Give me back XP!!!
Can someone maybe walk through what each part of that script does for me?
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
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.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
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. ;)
 

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

Jedi D

New Member
Joined
Apr 12, 2015
Messages
2 (0.00/day)
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! :)
 
Top