windows 使用windows cmd递归删除0KB文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4176962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:44:42  来源:igfitidea点击:

Recursively delete 0KB files using windows cmd

windowsfilecmd

提问by crodjer

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.

我有一些进程可以在目录及其子目录中创建一些 0KB 大小的文件。
如何使用 Windows 命令提示符从文件系统中删除文件?
将执行该任务的任何单个命令或脚本都将起作用。



我只能运行简单的 cmd 命令和脚本,在访问受限的远程机器上工作。

回答by Joey

  1. Iterate recursively over the files:

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

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

    del "%F"
    
  1. 递归遍历文件:

    for /r %F in (*)
    
  2. 找出零长度文件:

    if %~zF==0
    
  3. 删除它们:

    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 ifneeds to read if %~zF LSS 1000or whatever your threshold is.

注意:我假设您的意思是长度恰好为 0 字节的文件。如果 0 KB 意味着小于 1000 字节,那么上面if需要读取if %~zF LSS 1000或无论您的阈值是多少。

回答by Northover

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

发现于:链接文本似乎有效,但有一个警告:它不会删除名称包含空格的文件。可能有一个解决方法,但恐怕批处理不是我的强项。

回答by Thom Spengler

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

更正错别字后,这可以正常工作。问题是缺少波浪号 (~) 例如,del "%%a" 需要是 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

这确实会删除名称中带有空格的文件,因为它将标记括在“双引号”中 - 另一种方法是使用“短名称”,如第二个示例 [ %%~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 "%%~a" )

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

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

回答by Benoit

You can try find.exe from the UnxUtils.

您可以从UnxUtils尝试 find.exe 。

find . -type f -empty -delete