且构网

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

如何通过cmd调用删除文件夹中的所有文件和文件夹

更新时间:2021-09-22 02:28:09

不,我不认识.

如果您出于某种原因(ACL,& c.)想要保留原始目录,而实际上却想清空它,则可以执行以下操作:

If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:

del /q destination\*
for /d %x in (destination\*) do @rd /s /q "%x"

这首先从目录中删除所有文件,然后以递归方式删除所有嵌套目录,但总体上保持顶层目录不变(内容除外).

This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).

请注意,在批处理文件中,您需要将for循环中的%翻倍:

Note that within a batch file you need to double the % within the for loop:

del /q destination\*
for /d %%x in (destination\*) do @rd /s /q "%%x"