简体   繁体   English

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

[英]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.

相关问题 删除目录及其中的所有子目录 - removing directories and all sub-directories in it 仅在目录,子目录中读取最近2个月的文件 - Read files for last 2 months only in directory, sub-directories 将“Everyone”组添加到目录及其所有子目录中 - Add group “Everyone” to directory and all of it's sub-directories 尝试使用Bat文件获取目录和子目录中所有文件的行数 - Trying to Get A Line Count for all Files in a Directory and Sub-Directories using a Bat File ImageMagick 的批处理命令在 Windows 上转换目录和子目录中的所有文件 - Batch command for ImageMagick to convert all files in a directory and sub-directories on windows 如何使用批处理脚本从当前目录复制文件,而不复制子目录? - How do I copy files from the current directory, but not sub-directories using a batch script? 如何使用PowerShell为多个子目录创建目录连接? - How to create directory junctions for multiple sub-directories using PowerShell? 如何为文件夹中的所有子目录执行 git 拉取?(在 Windows 中) - How to do git pull for all sub-directories in a folder?(In Windows ) 批处理文件:列出目录中的所有文件,打印为.txt并将输出文件放置在所有子目录中 - Batch Files: List all files in a directory, print as .txt and place output file in all sub directories 如何在.bat文件(Windows)中使用ImageMagick调整所有子目录中所有图像的大小? - How would I use ImageMagick in a .bat file (Windows) to resize all images in all sub-directories?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM