[英]Remove All Sub-Directories and Sub-Files Without Removing Parent/Root Directory?
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?通过 Windows 批处理,删除文件夹的所有子目录和子文件而不删除/删除所述父/根文件夹的命令是什么?
Here's what I have tried so far:到目前为止,这是我尝试过的:
ECHO "Good riddance, cache! Muahahahahahaha"
cd "C:\Users\abrewer\Desktop\cache"
del * /q
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.
我也试过 RMDIR 和 RD,这两个命令似乎删除了父/根目录。
The simplest way of doing 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. 尝试删除父目录失败时,它会输出一条错误消息,但否则可以正常工作。
Alternatively, something like: 或者,类似:
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.
请记住,如果从命令行而不是在批处理文件中运行,请仅使用单个百分号。
@echo off
ECHO "Good riddance, cache! Muahahahahahaha"
pushd "C:\Users\abrewer\Desktop\cache"
del * /q
for /d %%F in (*) do rd /s /q "%%F"
popd
cd "C:\Users\abrewer\Desktop\cache"
if %errorlevel% neq 0 exit /b %errorlevel%
rd /s /q .
The first part of the top answer is great but it's also dangerous.最佳答案的第一部分很棒,但也很危险。 Please make sure you have some error checking in your code, as if the directory doesn't exist for some reason you're going to delete what's in your working path.
请确保您的代码中有一些错误检查,就好像该目录由于某种原因不存在您将要删除工作路径中的内容一样。
I'm too new to add a comment and the edit queue was full for the answer to suggest an amendment.我太新了,无法添加评论,编辑队列已满,无法提出建议修改的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.