首页 > 解决方案 > 使用 Windows 批处理文件删除所有早于今天的 *.Zip 文件

问题描述

假设您必须使用 Windows 批处理文件(not powershell),并且想要删除.zip当前活动目录中所有以结尾的文件。这个怎么做?

到目前为止的所有尝试都失败了:

forfiles -p "C:\temp\test" -s -m *.zip -d 1 -c "cmd /c del *.zip"

为此它说

ERROR: No files found with the specified search criteria.

标签: batch-file

解决方案


正如我在评论中所建议的那样,您的问题可以通过阅读命令的使用信息来轻松解决, (在命令提示符下输入时可用FORFILES /?) 。

根据您的问题标准,“delete all files ending in .zipthe current active directory”:

您不需要使用该/P选项,因为如使用信息中所述,“ The default folder is the current working directory (.)”。

您的问题中没有关于当前目录的递归子目录的任何内容,因此不需要/S" " 选项。Instructs forfiles to recurse into subdirectories

对于/D您正在查找上次修改日期小于昨天的文件的选项,即 " the current date minus "dd" days"/D -1

因为要删除当前目录中的文件,所以不需要使用 " Full path of the file", @path,所以你需要的是 " The name of the file", @file

FORFILES /M *.zip /D -1 /C "CMD /C DEL @file"

推荐阅读