删除所有子目录和子文件而不删除父/根目录?

问题描述:

通过Windows批处理,命令是删除文件夹的所有子目录和子文件夹,而不删除/删除所述父/根文件夹?

Via Windows Batch, what would the command be to remove all sub directories and sub files of a folder without deleting/removing the said parent/root folder?

我到目前为止所尝试的:

Here's what I have tried so far:

ECHO "Good riddance, cache! Muahahahahahaha"
cd "C:\Users\abrewer\Desktop\cache"
del * /q

仅删除文件,而不删除子文件夹。我也试过RMDIR和RD,这两个命令似乎删除父/根目录。

The above only removes files, but not sub folders. I have also tried RMDIR and RD, those two commands seem to remove the parent/root directory.

it:

cd "C:\Users\abrewer\Desktop\cache"
rd /s /q .

它尝试删除父目录时会输出错误消息,

It outputs an error message when it tries and fails to delete the parent directory, but otherwise it works perfectly.

或者,如下:

cd "C:\Users\abrewer\Desktop\cache"
del * /q
for /D %%i in (*) do rd /s /q "%%i"

可能会正常工作。如果您从命令行而不是批处理文件运行,请记住只使用单百分号。

might work. Remember to only use single percent signs if you're running from the command line rather than in a batch file.