简体   繁体   English

如何解决在批处理脚本中运行 rmdir 命令时出现“目录不为空”错误?

[英]How to solve "The directory is not empty" error when running rmdir command in a batch script?

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,但最终我再次遇到了错误。 Here's the command:这是命令:

rmdir /S /Q "C:\<dir>\"

I experienced the same issues as Harry Johnston has mentioned.我遇到了与 Harry Johnston 提到的相同的问题。 rmdir /s /q would complain that a directory was not empty even though /s is meant to do the emptying for you! rmdir /s /q会抱怨目录不是空的,即使/s是为了为您清空! I think it's a bug in Windows, personally.我个人认为这是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的标准输出,否则它会列出它删除的每个文件。)

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

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.这可以从搜索窗口框或 cmd 提示符运行。 The /F fixes any issues it finds, like recovering the files. /F修复它发现的任何问题,例如恢复文件。 Once this finishes running, you can delete the files and folders like normal.完成运行后,您可以像往常一样删除文件和文件夹。

以管理员身份输入命令提示符并运行

rmdir /s <FOLDER>

I had a similar problem, tried to delete an empty folder via windows explorer.我有一个类似的问题,试图通过 Windows 资源管理器删除一个空文件夹。 Showed me the not empty error, so I thought I try it via admin cmd, but none of the answers here helped.向我展示了非空错误,所以我想我通过 admin cmd 尝试它,但这里的答案都没有帮助。

After I moved a file into the empty folder.在我将文件移动到空文件夹后。 I was able to delete the non empty folder我能够删除非空文件夹

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.正如@gfullam 在对@BoffinbraiN 的回答的评论中所说,您要删除的<dir>本身可能不是包含文件的那个: <dir>中可能有子目录会收到“目录不是空的”消息,并且唯一的解决方案是递归遍历目录,手动删除所有包含的文件......我最终决定使用rm来自 UNIX 的端口。 rm.exe comes with Git Bash, MinGW, Cygwin, GnuWin32 and others. rm.exe与 Git Bash、MinGW、Cygwin、GnuWin32 等一起提供。 You just need to have its parent directory in your PATH and then execute as you would in a UNIX system.您只需要将其父目录放在 PATH 中,然后像在 UNIX 系统中一样执行。

Batch script example:批处理脚本示例:

set PATH=C:\cygwin64\bin;%PATH%
rm -rf "C:\<dir>"

I had "C:\Users\User Name\OneDrive\Fonts", which was mklink'ed ( /D ) to "C:\Windows\Fonts", and I got the same problem.我有“C:\Users\User Name\OneDrive\Fonts”,它被 mklink'ed ( /D ) 到“C:\Windows\Fonts”,我也遇到了同样的问题。 In my case就我而言

cd "C:\Users\User Name\OneDrive" cd "C:\用户\用户名\OneDrive"

rd /s Fonts rd /s 字体

Y (to confirm the action) Y(确认动作)

helped me.帮助过我。 I hope, that it helps you too ;D希望对你也有帮助 ;D

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

What worked for me is the following.对我有用的是以下内容。 I appears like the RMDir command will issue “The directory is not empty” nearly all the time...我看起来好像 RMDi 命令几乎总是会发出“目录不是空的”......

: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

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%

Open CMD as administrator以管理员身份打开 CMD

chkdsk c: /F /R
  • Press the “Y” key if asked to check your disk the next time your system restarts.如果系统下次重新启动时要求检查磁盘,请按“Y”键。

Restart the machine.重新启动机器。 After that just delete the folder.之后,只需删除该文件夹。

Similar to Harry Johnston's answer, I loop until it works.与 Harry Johnston 的回答类似,我循环直到它起作用。

set dirPath=C:\temp\mytest
:removedir
if exist "%dirPath%" (
    rd /s /q "%dirPath%" 
    goto removedir
)

Force delete the directory (if exists)强制删除目录(如果存在)

Delete.bat删除.bat

set output_path="C:\Temp\MyFolder"
    
if exist %output_path% (
   echo Deleting %output_path%
   attrib -r /s /d %output_path%
   rd /s /q %output_path%
)

one liner:一个班轮:

if exist folder rmdir /Q /S folder

I'm using this in a NPM script like so (Javascript) :我在这样的 NPM 脚本中使用它(Javascript):

//package.json
  "scripts": {
    "start": "parcel --no-cache",
    "clean": "if exist dist rmdir /Q /S dist",
    "deploy": "npm run clean && parcel build --no-source-maps && firebase deploy"
  },

I've fixed this before my making sure there wasn't extra whitespace in the name of the directory I was deleting.在确保要删除的目录名称中没有多余的空格之前,我已经解决了这个问题。 This is more of a concern when I had the directory name contained within a variable that I was passing to RD .当我将目录名称包含在传递给RD的变量中时,这更令人担忧。 If you're specifying your directly in quotes then this isn't helpful, but I hope that someone like me comes along with the same problem and sees this.如果您直接在引号中指定您的,那么这没有帮助,但我希望像我这样的人遇到同样的问题并看到这一点。 RD /S /Q can work, as I noticed the issue started happening when I changed something in my batch script. RD /S /Q可以工作,因为我注意到当我更改批处理脚本中的某些内容时问题开始发生。

I can think of the following possible causes:我可以想到以下可能的原因:

  1. there are files or subdirectories which need higher permissions有需要更高权限的文件或子目录
  2. there are files in use, not only by WSearch, but maybe by your virus scanner or anything else有文件正在使用,不仅由 WSearch,而且可能由您的病毒扫描程序或其他任何东西使用

For 1.) you can try runas /user:Administrator in order to get higher privileges or start the batch file as administrator via context menu.对于 1.) 您可以尝试runas /user:Administrator以获得更高的权限或通过上下文菜单以管理员身份启动批处理文件。 If that doesn't help, maybe even the administrator doesn't have the rights.如果这没有帮助,也许甚至管理员也没有权限。 Then you need to take over the ownership of the directory.然后你需要接管目录的所有权。

For 2.) download Process Explorer , click Find/Find handle or DLL... or press Ctrl+F , type the name of the directory and find out who uses it.对于 2.) 下载Process Explorer ,单击Find/Find handle or DLL...或按Ctrl+F ,键入目录的名称并找出使用它的人。 Close the application which uses the directory, if possible.如果可能,请关闭使用该目录的应用程序。

rmdir <directory> /s

这对我有用。

Windows sometimes is "broken by design", so you need to create an empty folder, and then mirror the "broken folder" with an "empty folder" with backup mode. Windows 有时是“被设计破坏的”,因此您需要创建一个空文件夹,然后将“损坏的文件夹”与具有备份模式的“空文件夹”镜像。

robocopy - cmd copy utility

/copyall - copies everything
/mir deletes item if there is no such item in source a.k.a mirrors source with
destination
/b works around premissions shenanigans

Create en empty dir like this:像这样创建一个空目录:

mkdir empty

overwrite broken folder with empty like this:用空覆盖损坏的文件夹,如下所示:

robocopy /copyall /mir /b empty broken

and then delete that folder然后删除该文件夹

rd broken /s
rd empty /s

If this does not help, try restarting in "recovery mode with command prompt" by holding shift when clicking restart and trying to run these command again in recovery mode如果这没有帮助,请尝试在“使用命令提示符的恢复模式”中重新启动,方法是在单击重新启动时按住 shift 并尝试在恢复模式下再次运行这些命令

The easiest way I found to do this is:我发现这样做的最简单方法是:

rm -rf dir_name

it worked on zsh - macOS, it should work on windows cmd as well.它适用于 zsh - macOS,它也应该适用于 windows cmd。

if you need to delete a folder on Windows with a batch file you will need to use PowerShell and this is how it is done:如果您需要使用批处理文件删除Windows上的文件夹,您将需要使用 PowerShell,这是如何完成的:

rmdir .\directory_name\ -Recurse

This occurs because you are IN the directory. 发生这种情况是因为您在目录中。 Make sure your CLI and explorer are not in the directory. 确保您的CLI和资源管理器不在目录中。 Close all of them to be sure. 关闭所有这些以确定。 Then run as admin and cd to the parent directory and run: 然后以admin身份运行并cd到父目录并运行:

rmdir /s /q mydir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM