Windows 7 从命令行清空整个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5900608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows 7 empty entire directory from command line
提问by UpHelix
I am running a batch file which deploys a war file to a specific directory. However, I want to empty that directory before deploying to it, recursively.
我正在运行一个批处理文件,它将一个War文件部署到一个特定的目录。但是,我想在递归部署之前清空该目录。
I found this script that is supposed to remove everything in the directory including folders and files but I get this error: "%%i was unexpected at this time."
我发现这个脚本应该删除目录中的所有内容,包括文件夹和文件,但我收到此错误:“%%i 此时出乎意料。”
FOR /D %%i IN ("C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*") DO RD /S /Q "%%i" DEL /Q "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*.*"
I just need a simple way to recursively empty my ROOT directory either by fixing the script above or with another method. Have to be able to do this from a batch file.
我只需要一种简单的方法来通过修复上面的脚本或使用其他方法递归地清空我的 ROOT 目录。必须能够从批处理文件中执行此操作。
I am running windows 7.
我正在运行 Windows 7。
SOLUTION
解决方案
rmdir ROOT /s /q
mkdir ROOT
回答by mkly
I think what you are looking for is to clear the folder not to delete it and recreate it. This will actualy clear the folder so you don't have to recreate it.
我认为您正在寻找的是清除文件夹而不是删除它并重新创建它。这实际上会清除文件夹,因此您不必重新创建它。
CD "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*
A few notes.
You only use a single % when running from the command line. doubles are for batch files.
&& is for running multiple commands on the same line.
* The &&'s are especially important in this case because you do not want the del /q .to run if the cd fails as you will delete to contents of what ever directory you started in.
一些笔记。
从命令行运行时,您只使用一个 %。doubles 用于批处理文件。
&& 用于在同一行上运行多个命令。
* 在这种情况下 && 尤其重要,因为您不想要 del /q 。如果 cd 失败,则运行,因为您将删除您启动的目录的内容。
回答by Red
Try this:
尝试这个:
forfiles /p "folder" /s /c "cmd /c rd /q @path
forfiles /p "文件夹" /s /c "cmd /c rd /q @path
replace "folder" with your folder name or drive letter
用您的文件夹名称或驱动器号替换“文件夹”
example: forfiles /p f: /s /c "cmd /c rd /q @path
例如:forfiles /pf: /s /c "cmd /c rd /q @path
This will recurse through all subfolders, and only remove the empty folders. If you use it in command line, you'll get error messages for folders that aren't empty. If you put it in a batch script, it will take some time to run, depending on how many folders you have.
这将遍历所有子文件夹,并且只删除空文件夹。如果您在命令行中使用它,您将收到非空文件夹的错误消息。如果你把它放在一个批处理脚本中,它需要一些时间来运行,这取决于你有多少文件夹。