Windows 7 从命令行清空整个目录

发布于 2024-11-05 18:17:19 字数 523 浏览 0 评论 0原文

我正在运行一个批处理文件,它将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-11-12 18:17:19

您应该使用 rmdir /S。查看此链接

You should use rmdir /S. Check out this link

落叶缤纷 2024-11-12 18:17:19

我认为您要寻找的是清除文件夹而不是删除它并重新创建它。这实际上会清除该文件夹,因此您不必重新创建它。

CD "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*  

一些注释。
从命令行运行时仅使用单个%。双打用于批处理文件。
&&用于在同一行上运行多个命令。
* 在这种情况下, && 尤为重要,因为如果 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.

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.

夕色琉璃 2024-11-12 18:17:19

试试这个:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文