如何通过cmd调用删除文件夹中的所有文件和文件夹 [英] How to delete all files and folders in a folder by cmd call
问题描述
我使用Windows.
I use Windows.
我想通过系统调用删除文件夹中的所有文件和文件夹.
I want to delete all files and folders in a folder by system call.
我可能会这样打电话:
>rd /s /q c:\destination
>md c:\destination
您知道更简单的方法吗?
Do you know an easier way?
推荐答案
不,我不认识.
如果您出于某种原因(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"
这篇关于如何通过cmd调用删除文件夹中的所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!