简体   繁体   中英

Recursively delete 0KB files using windows cmd

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.

  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.

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

This works fine when a typo is corrected. The problem was a missing tilde (~) eg, 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 )

您可以尝试从UnxUtils中找到find.exe。

find . -type f -empty -delete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM