Windows 7 从命令行清空整个目录
我正在运行一个批处理文件,它将 war 文件部署到特定目录。但是,我想在递归部署到该目录之前清空该目录。
我发现这个脚本应该删除目录中的所有内容,包括文件夹和文件,但我收到此错误:“%%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/*.*"
我只需要一种简单的方法来通过修复上面的脚本或使用其他方法来递归清空我的 ROOT 目录。必须能够从批处理文件中执行此操作。
我运行的是 Windows 7。
解决方案
rmdir ROOT /s /q
mkdir ROOT
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.
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."
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.
I am running windows 7.
SOLUTION
rmdir ROOT /s /q
mkdir ROOT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 rmdir /S。查看此链接
You should use rmdir /S. Check out this link
我认为您要寻找的是清除文件夹而不是删除它并重新创建它。这实际上会清除该文件夹,因此您不必重新创建它。
一些注释。
从命令行运行时仅使用单个%。双打用于批处理文件。
&&用于在同一行上运行多个命令。
* 在这种情况下, && 尤为重要,因为如果 cd 失败,您不希望 del /q . 运行,因为您将删除您启动的目录的内容。
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.
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.
试试这个:
forfiles /p "folder" /s /c "cmd /c rd /q @path
用您的文件夹名称或驱动器号替换 "folder"
例如: forfiles /pf: /s /c "cmd /c rd /q @path
这将递归遍历所有子文件夹,并且仅删除空文件夹。
如果您在命令行中使用它,您将收到非空文件夹的错误消息。如果将其放入批处理脚本中,则需要一些时间才能运行,具体取决于您拥有的文件夹数量。
Try this:
forfiles /p "folder" /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
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.