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

时间:2021-12-11 02:28:31

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,
  • 我需要删除X类型的文件,

  • I have the parent folder C:\MyProject,
  • 我有父文件夹C:\ MyProject,

  • There are Y subfolders C:\MyProject\?,
  • 有Y个子文件夹C:\ MyProject \?,

  • There are N files to delete.
  • 有N个文件要删除。

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

有一个快速del(类型)功能,我只是缺少?

2 个解决方案

#1


33  

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的所有子文件夹。

#2


1  

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 \\\"\" $0 \"\\*.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类型的文件),并使用缺少递归标记的命令。

#1


33  

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的所有子文件夹。

#2


1  

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 \\\"\" $0 \"\\*.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类型的文件),并使用缺少递归标记的命令。