如何从 Windows 批处理文件的 Y 子文件夹中删除 X 类型的 N 个文件?

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

How to delete N files of X type from Y subfolders from a Windows batch file?

windowsfile-iocommand-linecommand-prompt

提问by Jeremiah

I'm trying to write a windows batch file that can delete files from subdirectories. I would rather not hard code the directory structure in, so I can use this process with other projects.

我正在尝试编写一个可以从子目录中删除文件的 Windows 批处理文件。我宁愿不对目录结构进行硬编码,这样我就可以将此过程用于其他项目。

  • I need to delete files of X type,
  • I have the parent folder C:\MyProject,
  • There are Y subfolders C:\MyProject\?,
  • There are N files to delete.
  • 我需要删除X类型的文件,
  • 我有父文件夹C:\MyProject
  • 有 Y 个子文件夹C:\MyProject\?
  • 有 N 个文件要删除。

Is there a quick del(of type) function I am simply missing?

是否有del我只是缺少的快速(类型)功能?

回答by Pedrin

Actually you can use the standard del command:

实际上你可以使用标准的 del 命令:

c:
cd MyProject
del /S *.type

Where type is the extension you want to delete and the /S parameter will check in all subfolders of MyProject.

其中 type 是您要删除的扩展名,/S 参数将检查 MyProject 的所有子文件夹。

回答by Joe Pineda

If the del command didn't have the /S flag to delete recursively, I'd use AWK to do something like this (you'd need the UNIX tools for Windows):

如果 del 命令没有 /S 标志来递归删除,我会使用 AWK 来做这样的事情(你需要适用于 Windows 的 UNIX 工具):

dir MyProject\*.* /ad /s /b | gawk "{print \"del \\"\" ##代码## \"\*.type\\"\";}" | cmd

My 2 cents, in case you ever need to do something similar (applying a program to all files of X type in all subfolders) with a command that lacks a recursive flag.

我的 2 美分,以防您需要使用缺少递归标志的命令执行类似操作(将程序应用于所有子文件夹中的所有 X 类型文件)。