# Delete files based on Size



## IGGt (Nov 10, 2009)

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 (Jul 29, 2001)

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 (Aug 8, 2007)

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:

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

```
@Echo Off
Set _Source=C:\My Folder
PushD "%_Source%"
For /F "tokens=*" %%A In ('Dir /OS /A-D /B') Do If %%~zA==0 [COLOR=Red]Echo[/COLOR] 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.


```
@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 (
[COLOR=Red]Echo[/COLOR] Del %%A
))
:_Done
PopD
```


----------

