windows 批处理 - 在 rmdir 命令上获取“目录不为空”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22948189/
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
Batch - Getting "The directory is not empty" on rmdir command
提问by Mayhem
I am making a batch script and part of the script is trying to remove a directory and all of its sub-directories. I am getting an intermittent error about a sub-directory not being empty. I read one article about indexing being the culprit. I disabled WSearch but I eventually got the error again.
我正在制作一个批处理脚本,脚本的一部分正在尝试删除一个目录及其所有子目录。我收到关于子目录不为空的间歇性错误。我读了一篇关于索引是罪魁祸首的文章。我禁用了 WSearch,但我最终再次出现错误。
rmdir /S /Q "C:\<dir>\"
回答by BoffinBrain
I experienced the same issues as Harry Johnston has mentioned. rmdir /s /q
would complain that a directory was not empty even though /s
is meant to do the emptying for you! I think it's a bug in Windows, personally.
我遇到了与 Harry Johnston 提到的相同的问题。rmdir /s /q
会抱怨目录不是空的,即使/s
是为了为你清空!我个人认为这是 Windows 中的一个错误。
My workaround is to del
everything in the directory before deleting the directory itself:
我的解决方法是del
在删除目录本身之前删除目录中的所有内容:
del /f /s /q mydir 1>nul
rmdir /s /q mydir
(The 1>nul
hides the standard output of del
because otherwise, it lists every single file it deletes.)
(1>nul
隐藏标准输出,del
因为否则,它会列出它删除的每个文件。)
回答by Harry Johnston
I'm familiar with this problem. The simplest workaround is to conditionally repeat the operation. I've never seen it fail twice in a row - unless there actually is an open file or a permissions issue, obviously!
我对这个问题很熟悉。最简单的解决方法是有条件地重复操作。我从未见过它连续失败两次 - 除非确实存在打开的文件或权限问题,显然!
rd /s /q c:\deleteme
if exist c:\deleteme rd /s /q c:\deleteme
回答by Adilson Cabral
enter the Command Prompt as Admin and run
以管理员身份输入命令提示符并运行
rmdir /s <FOLDER>
回答by jrose
I just encountered the same problem and it had to do with some files being lost or corrupted. To correct the issue, just run check disk:
我刚刚遇到了同样的问题,它与某些文件丢失或损坏有关。要解决此问题,只需运行检查磁盘:
chkdsk /F e:
This can be run from the search windows box or from a cmd prompt. The /F
fixes any issues it finds, like recovering the files. Once this finishes running, you can delete the files and folders like normal.
这可以从搜索窗口框或从 cmd 提示符运行。该/F
修复它发现,像恢复文件的任何问题。运行完成后,您可以像往常一样删除文件和文件夹。
回答by Grisu118
I had a similar problem, tried to delete an empty folder via windows explorer. Showed me the not empty error, so I thought I try it via admin cmd, but none of the answers here helped.
我有一个类似的问题,试图通过 Windows 资源管理器删除一个空文件夹。向我展示了非空错误,所以我想我是通过 admin cmd 尝试的,但这里的答案都没有帮助。
After I moved a file into the empty folder. I was able to delete the non empty folder
在我将文件移动到空文件夹后。我能够删除非空文件夹
回答by Gobe
As @gfullam stated in a comment to @BoffinbraiN's answer, the <dir>
you are deleting itself might not be the one which contains files: there might be subdirectories in <dir>
that get a "The directory is not empty" message and the only solution then would be to recursively iterate over the directories, manually deleting all their containing files... I ended up deciding to use a port of rm
from UNIX. rm.exe
comes with Git Bash, MinGW, Cygwin, GnuWin32 and others. You just need to have its parent directory in your PATH and then execute as you would in a UNIX system.
正如@gfullam 在对@BoffinbraiN 的回答的评论中所说的那样,<dir>
您要删除的本身可能不是包含文件的文件:其中可能有子目录<dir>
会收到“目录不为空”消息,那么唯一的解决方案是递归遍历目录,手动删除它们的所有包含文件......我最终决定使用rm
来自 UNIX的端口。rm.exe
附带 Git Bash、MinGW、Cygwin、GnuWin32 等。你只需要在你的 PATH 中有它的父目录,然后像在 UNIX 系统中一样执行。
Batch script example:
批处理脚本示例:
set PATH=C:\cygwin64\bin;%PATH%
rm -rf "C:\<dir>"
回答by user7432246
What worked for me is the following. I appears like the RMDir command will issue “The directory is not empty” nearly all the time...
对我有用的是以下内容。我似乎 RMDir 命令几乎一直都会发出“目录不为空”...
:Cleanup_Temporary_Files_and_Folders
Erase /F /S /Q C:\MyDir
RMDir /S /Q C:\MyDir
If Exist C:\MyDir GoTo Cleanup_Temporary_Files_and_Folders
回答by Daniel Barde
Im my case i just moved the folder to root directory like so.
我的情况我只是像这样将文件夹移动到根目录。
move <source directory> c:\
And then ran the command to remove the directory
然后运行命令删除目录
rmdir c:\<moved directory> /s /q
回答by Maciej Bled
I had "C:\Users\User Name\OneDrive\Fonts", which was mklink'ed ( /D ) to "C:\Windows\Fonts", and I was the same problem. In my case
我有“C:\Users\User Name\OneDrive\Fonts”,它是 mklink'ed ( /D ) 到“C:\Windows\Fonts”,我遇到了同样的问题。就我而言
cd "C:\Users\User Name\OneDrive"
rd /s Fonts
Y (to confirm the action)
cd "C:\用户\用户名\OneDrive"
rd / s 字体
Y(确认动作)
helped me. I hope, that it helps you too ;D
帮助过我。我希望,它也能帮助你;D
回答by Peter Hoeg
The reason rd /s
refuses to delete certain files is most likely due to READONLY file attributes on files in the directory.
rd /s
拒绝删除某些文件的原因很可能是由于目录中文件的 READONLY 文件属性。
The proper way to fix this, is to make sure you reset the attributes on all files first:
解决此问题的正确方法是确保首先重置所有文件的属性:
attrib -r %directory% /s /d
rd /s %directory%
There could be others such as hidden or system files, so if you want to play it safe:
可能还有其他文件,例如隐藏文件或系统文件,因此如果您想安全使用:
attrib -h -r -s %directory% /s /d
rd /s %directory%