Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Windows Server
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Windows Server >
Delete files based on Size

Reply  
Thread Tools
IGGt's Avatar
Junior Member with 1 posts.
 
Join Date: Nov 2009
Experience: Intermediate
10-Nov-2009, 06:43 AM #1
Delete files based on Size
Is there a way to write a simple Batch file that would allow me to delete all files in a folder with a size of 0KB.

I have an application that churns out csv files every two minutes, but quite a few are empty, and I want to get rid of them automatically before archiving the rest.
Rockn's Avatar
Computer Specs
Distinguished Member with 21,325 posts.
 
Join Date: Jul 2001
Location: Somalia of the North, MN
Experience: Disenfranchised American
10-Nov-2009, 01:50 PM #2
You could use robocopy to copy the files to another directory and then delete all files in that folder. It could all be batched and set up as an automated task.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Nov-2009, 08:08 PM #3
Welcome to TSG!

Robocopy won't let you specify a max size of zero, it has to be at least 1, which will also copy/move 1 byte files.
If that will work, this will move all files size 0 or 1, then delete them:
Code:
Set _Source=C:\My Folder
Set _Dest=C:\My Temp
Robocopy "%_Source%" "%_Dest%" /Max:1 /MOV
Del %_Dest%\*.* /Q
Otherwise you can use a For loop and Dir to Delete the files with a 0 byte size. I've put an Echo statement in front of the Del command for testing, remove it when you are satisfied it will delete only what you want deleted:
Code:
@Echo Off
Set _Source=C:\My Folder
PushD "%_Source%"
For /F "tokens=*" %%A In ('Dir /OS /A-D /B') Do If %%~zA==0 Echo Del %%A
PopD
This next version will exit after processing the 0 byte files, so may be faster. May not get them all though.

Because of the way a For loop works, you could get a Dir that sees a file as zero bytes because it was just created, but by the time it's size gets checked by the If statement, it's no longer 0, so the loop would end before all the 0 byte files were deleted.

Some system files may show as zero even when they aren't as well.
If you run this in the Windows folder, Dir sees the wiaservc.log as a zero byte file, but it's actually 49 bytes on my system. So out of 6 zero byte files, it only deletes 2 files.

Code:
@Echo Off
Set _Source=C:\My Folder
PushD "%_Source%"
For /F "tokens=*" %%A In ('Dir /OS /A-D /B') Do (
If %%~zA GTR 0 (
Goto :_Done
) Else (
Echo Del %%A
))
:_Done
PopD
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:08 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.