23

I have some process which creates some files of 0KB size in a directory and its sub-directories.
How can I delete the files from the file system using the windows command prompt?
Any single command or a script that will do the task will work.


I can only run simple cmd commands and scripts, working on a remote machine with restricted access.
crodjer
  • 13,384
  • 9
  • 38
  • 52
  • 1
    What version of Windows? Many versions of Windows include VBScript which might be more flexible... http://www.suite101.com/content/windows-scripting-command-line-vbscript-a83052 – Klinky Nov 14 '10 at 10:21
  • I would use `for` to scan dir tree for files, `findstr` to search files for "." pattern and delete those non-matching. – Constantin Nov 14 '10 at 10:32

4 Answers4

53
  1. Iterate recursively over the files:

    for /r %F in (*)
    
  2. Find out zero-length files:

    if %~zF==0
    
  3. Delete them:

    del "%F"
    

Putting it all together:

for /r %F in (*) do if %~zF==0 del "%F"

If you need this in a batch file, then you need to double the %:

for /r %%F in (*) do if %%~zF==0 del "%%F"

Note: I was assuming that you meant files of exactly 0 Bytes in length. If with 0 KB you mean anything less than 1000 bytes, then above if needs to read if %~zF LSS 1000 or whatever your threshold is.

Joey
  • 344,408
  • 85
  • 689
  • 683
2
@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do (
if %%~Za equ 0 del "%%a"
)

Found at : link text seems to work, with one caveat: It won't delete files with names containing spaces. There may be a work-around, but I'm afraid batch is not my forte.

Northover
  • 981
  • 8
  • 14
  • Well, since you explicitly declare space to be a token delimiter `for` *will* tokenize. I guess, `del "%%a %%b"` should work. But the actual answer is that you should never use `for /f` over the output of `dir` unless it is absolutely necessary (e.g. for sorting). `for` itself can iterate over files and is much more robust that way. For example, your solution won't handle file names that contain Unicode characters with no representation in the OEM codepage, if Raster fonts are selected for the console window. Those interactions are subtle but I tend to stay far away from `for /f` for filenames – Joey Nov 14 '10 at 14:48
0

This works fine when a typo is corrected. The problem was a missing tilde (~) e.g., del "%%a" needs to be del "%%~a"

This will indeed delete files with spaces in the name because it encloses the token in "double quotes" - an alternate method would be to use 'short name' as shown in the second example [ %%~sa

@echo off setLocal EnableDelayedExpansion for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do ( if %%~Za equ 0 del "%%~a" )

@echo off setLocal EnableDelayedExpansion for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do ( if %%~Za equ 0 del %%~sa )

-1

You can try find.exe from the UnxUtils.

find . -type f -empty -delete
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Its not useful for this purpose....working a remote machine...so no direct access. Can only run cmd commands and scripts. – crodjer Nov 14 '10 at 10:06
  • I think this answer shouldn't have been down-voted. It still provides a useful bit of information, probably for slightly different contexts. Up-voted to neutralize it. – crodjer May 24 '13 at 17:20