• We've upgraded our forums. Please post any issues/requests in this thread.

BAT File That Deletes Files Below Specific Size

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.06/day)
Likes
8
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
#1
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

New Member
Joined
Aug 25, 2008
Messages
349 (0.10/day)
Likes
55
Location
Hampton, VA USA
System Name ambalamps
Processor i7 980 @ 4.15 GHZ
Motherboard Gigabyte x58a UD3R
Cooling Corsair h70
Memory 12 GB Dominator DDR3 1600
Video Card(s) GTX580'S in SLI
Storage OCZ Vertex Turbo 60GB WD Velociraptor 300GB Seagate Barracuda XT 2 TB
Display(s) Asus 27" LCD
Case HAF-X
Audio Device(s) X-FI Titanium HD
Power Supply Coolermaster Real Power Pro 1kw Non Modular
Software Windows 7 x64 Pro
Benchmark Scores pwning n00bs like you
#2
@Echo off
pushd "MY Directory"
for %%j in (*) do if %%~zj gtr 1048576 del "%%~j"
popd
 

Mussels

Moderprator
Staff member
Joined
Oct 6, 2004
Messages
46,122 (9.57/day)
Likes
13,550
Location
Australalalalalaia.
System Name Daddy Long Legs
Processor Ryzen R7 1700, 3.9GHz 1.375v
Motherboard MSI X370 Gaming PRO carbon
Cooling Fractal Celsius S24 (Silent fans, meh pump)
Memory 16GB 2133 generic @ 2800
Video Card(s) MSI GTX 1080 Gaming X (BIOS modded to Gaming Z - faster and solved black screen bugs!)
Storage 1TB Intel SSD Pro 6000p (60TB USB3 storage)
Display(s) Samsung 4K 40" HDTV (UA40KU6000WXXY) / 27" Qnix 2K 110Hz
Case Fractal Design R5. So much room, so quiet...
Audio Device(s) Pioneer VSX-519V + Yamaha YHT-270 / sennheiser HD595/518 + bob marley zion's
Power Supply Corsair HX 750i (Platinum, fan off til 300W)
Mouse Logitech G403 + KKmoon desk-sized mousepad
Keyboard Corsair K65 Rapidfire
Software Windows 10 pro x64 (all systems)
Benchmark Scores Laptops: i7-4510U + 840M 2GB (touchscreen) 275GB SSD + 16GB i7-2630QM + GT 540M + 8GB
#3
why not just sort the folder by file size and manually delete all the tiny ones every now and then?
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#4
@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.06/day)
Likes
8
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
#5
@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

New Member
Joined
Aug 25, 2008
Messages
349 (0.10/day)
Likes
55
Location
Hampton, VA USA
System Name ambalamps
Processor i7 980 @ 4.15 GHZ
Motherboard Gigabyte x58a UD3R
Cooling Corsair h70
Memory 12 GB Dominator DDR3 1600
Video Card(s) GTX580'S in SLI
Storage OCZ Vertex Turbo 60GB WD Velociraptor 300GB Seagate Barracuda XT 2 TB
Display(s) Asus 27" LCD
Case HAF-X
Audio Device(s) X-FI Titanium HD
Power Supply Coolermaster Real Power Pro 1kw Non Modular
Software Windows 7 x64 Pro
Benchmark Scores pwning n00bs like you
#6
@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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#7
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
681 (0.17/day)
Likes
83
Processor AMD Phenom II x6 1090T @ 3.8GHz
Motherboard ASUS Sabertooth 990FX
Cooling Corsair H80i GTX
Memory Patriot Gamer 2 Series 8GB DDR3-1600
Video Card(s) XFX Radeon RX 460 4GB
Storage 1TB WD Red
Display(s) 1920x1080 HDTV
Case NZXT Phantom 240
Power Supply Corsair CX750M 750w
Software Win7 Professional though I hate it, I want XP back!
#8
Can someone maybe walk through what each part of that script does for me?
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#9
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#10
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.06/day)
Likes
8
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
#11
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)
Likes
0
#12
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! :)