且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用批处理脚本删除子文件夹中的文件

更新时间:2023-09-26 15:22:58

您可以使用 / S 开关删除在子文件夹中删除也是如此。

You can use the /s switch for del to delete in subfolders as well.

示例

del D:\test\*.* /s

会删除所有文件测试,包括所有子文件夹中的所有文件。

Would delete all files under test including all files in all subfolders.

要删除文件夹使用 RD ,同一交换机适用。

To remove folders use rd, same switch applies.

rd D:\test\folder /s /q

RD 不支持通配符 * 虽然如此,如果你想删除递归在测试目录可以使用循环。

rd doesn't support wildcards * though so if you want to recursively delete all subfolders under the test directory you can use a for loop.

for /r /d D:\test %a in (*) do rd %a /s /q

如果您使用的是一个批处理文件中的选项记得用2的,而不是1

If you are using the for option in a batch file remember to use 2 %'s instead of 1.