windows 删除小于特定大小的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6764621/
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 08:35:44  来源:igfitidea点击:

Delete files less than a specific size

windowsbatch-filecmd

提问by l--''''''---------''''''''''''

I would like to delete all files that are less than a specific size in a directory. Does anyone know if there is a Windows command that will do this? something like del *.* where size<3kb

我想删除目录中小于特定大小的所有文件。有谁知道是否有 Windows 命令可以执行此操作?就像是del *.* where size<3kb

I am currently doing this:

我目前正在这样做:

for /F %%A in ("*.pdf") do If %%~zA LSS 20103409 del %%~fA

and the ouput I get is:

我得到的输出是:

C:\Documents and Settings\agordon\Desktop\test>If 6440450 LSS 20103409 del C:\Do
cuments and Settings\agordon\Desktop\test\US Tox 01-06-11.pdf
The system cannot find the path specified.

...even though that PDF file is small enough to be deleted.

...即使该 PDF 文件小到可以删除。

What am I doing wrong?

我究竟做错了什么?

This is actually working:

这实际上是有效的:

FOR %%F IN (*.pdf) DO (
IF %%~zF LSS 20103409  DEL %%F
)

However it is not recognizing the file names because they have spaces! How do I convert the Windows name to a "DOS" name in that script? For example, the Windows name is file name.pdfI would probably need to convert to "DOS" and it would look like this file_name.pdfor something like that.

但是它无法识别文件名,因为它们有空格!如何在该脚本中将 Windows 名称转换为“DOS”名称?例如,Windows 名称是file name.pdf我可能需要转换为“DOS”,它看起来像这样file_name.pdf或那样。

回答by Mrchief

Try this from a batch script:

从批处理脚本中试试这个:

@echo off
setlocal
for /f  "usebackq delims=;" %%A in (`dir /b *.pdf`) do If %%~zA LSS 3145728 del "%%A"

回答by Rob Moore

Ok so I used AtomicParsley in a cmd script to add artwork to all my MP4 movies in over 400 sub folders with the --overWrite option. It sometimes barfs and the orignal file is left at less than 2k. I needed a way to find all these messed up MP4 files so I can redownload them.

好的,所以我在 cmd 脚本中使用了 AtomicParsley,使用 --overWrite 选项将插图添加到 400 多个子文件夹中的所有 MP4 电影中。它有时会导致原始文件少于 2k。我需要一种方法来找到所有这些乱七八糟的 MP4 文件,以便我可以重新下载它们。

This is what I used from the command line using Mrchief's example and it works like a charm in windows 7.

这是我使用 Mrchief 的示例从命令行使用的,它在 Windows 7 中的作用就像一个魅力。

@for /f  "usebackq delims=;" %A in (`dir /s /b *.mp4`) do @If %~zA LSS 2048 echo "%A"

and this is the output after processing 32 of the files

这是处理 32 个文件后的输出

    D:\Movie>@for /f  "usebackq delims=;" %A in (`dir /s /b *.mp4`) do @If %~zA LSS 2048 echo "%A"
    "D:\Movie\Action\Deadland (2009)\Deadland (2009).mp4"
    "D:\Movie\Science Fiction\Alien3 (1992)\Alien3 (1992).mp4"

    D:\Movie> 

you could replace the echo with del and change the 2048 to search for a different size.

您可以用 del 替换 echo 并更改 2048 以搜索不同的大小。

回答by aschipfl

Here is a different approach using robocopyand its filter capabilities. Here is an excerpt of the File Selection Optionsshown when robocopy /?is typed into the command prompt window:

这是一种不同的使用方法robocopy及其过滤功能。以下是在命令提示符窗口中键入File Selection Options时显示的摘录robocopy /?

             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900 then n = n days, else n = YYYYMMDD date).
             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900 then n = n days, else n = YYYYMMDD date).

Hence the /MINand /MAXoptions can be applied here. Since we do not want to copy any files, use the /Loption to list all the items that would be copied without the switch, then parse the returned list by a for /Floop, which holds the actual deletion command delin the body:

因此可以在此处应用/MIN/MAX选项。由于我们不想复制任何文件,因此使用/L选项列出所有将在没有开关的情况下复制的项目,然后通过for /F循环解析返回的列表,该循环将实际删除命令保存del在正文中:

set "TARGETDIR=."
set "FILES=*.pdf"
for /F "tokens=*" %%F in ('
    robocopy "%TARGETDIR%" "%TARGETDIR%" "%FILES%" ^
        /L /IS /FP /NC /NS /NDL /NP /NJH /NJS ^
        /MIN:0 /MAX:3071
') do (
    ECHO del "%%F"
)

After having tested, remove the upper-case ECHOfrom the script to actually delete files.

测试后,ECHO从脚本中删除大写字母以实际删除文件。

Besides /MIN, /MAXand /L, there are several other options defined in the robocopycommand line, most of which care for the needed output, namely a simple list of full paths of matching files, without any extra information like headers, footers or summaries.

除了/MIN,/MAX/Lrobocopy命令行中还定义了其他几个选项,其中大部分关心所需的输出,即匹配文件的完整路径的简单列表,没有任何额外的信息,如页眉、页脚或摘要。

The source and destination directories are both set to our target directory. Normally this would not work, of course (you cannot copy files onto themselves), but since /Lis stated, the file list is generated, but only if the switch /ISis given too (meaning that "same files" are to be regarded).

源目录和目标目录都设置为我们的目标目录。通常这当然是行不通的(您不能将文件复制到它们自己上),但是既然/L已经说明,文件列表就会生成,但前提/IS是也给出了开关(意味着要考虑“相同的文件”)。

回答by AndreDurao

BTW: Using PowerShell (on Windows 10) was the easier way for me:

顺便说一句:使用 PowerShell(在 Windows 10 上)对我来说是更简单的方法:

I only did this on the directory where I wish to delete files smaller than 20MB:

我只在我希望删除小于 20MB 的文件的目录上执行此操作:

ls | where {$_.Length -lt 20mb} | Remove-Item

回答by acpmasquerade

There isn't a switch in the Del command which will delete files based on filesize.

Del 命令中没有开关会根据文件大小删除文件。

回答by diagonalbatman

I am not sure how to do this from DOS, but this is something we have used in the past; it uses VBScript, so you would call this filesoversize.vbs.

我不确定如何从 DOS 执行此操作,但这是我们过去使用过的;它使用 VBScript,因此您可以将其称为 filesoversize.vbs。

Dim strPath
Dim lngSize

Dim fso
Dim fold
Dim files
Dim file

lngSize = 10000 ' The threshold in bytes (this is 100k)
strPath = "C:\temp\" 'The folder you want to start in

Set fso = CreateObject("scripting.filesystemobject")
Set fold = fso.GetFolder(strPath)
Set files = fold.files

For Each file In files
    If file.Size <= lngSize Then file.Delete True
Next