I'm trying to execute rd /S on a non-empty directory and sometimes, it works fine, sometimes, it reports "The directory is not empty". Why am I getting this error? Is there a reliable way to remove a folder full of files and subdirectories under Windows? PowerShell's Remove-Item also suffers from this issue.
4 Answers
It may be the local anti-virus or indexing service scanning and locking the newly copied files from being instantly deleted.
I would program a delay and retry on failure.
for (i = 1 to 10 etc...)
{ try to remove directory if directory does not exist, break out of loop wait a second
}
if directory still exists, abort! 1 It is quite likely that the undeleted empty folder contains some hidden files or that someone is still using the folder.
Try this:
del c:\test\*.* /Q
rmdir c:\test /Q 1 I'm trying to execute
rd /Son a non-empty directory and sometimes, it works fine, sometimes, it reports The directory is not empty. Why am I getting this error?
Because it contains hidden files or files requiring different permissions.
Is there a reliable way to remove a folder full of files and subdirectories under Windows?
Look inside the folder to see what files are left and check if you can manually delete them. If not, you may need different permissions. Try running it from an elevated command-prompt.
PowerShell's Remove-Item also suffers from this issue.
If it’s not a permission issue, you can force it: Remove-Item -Recurse -Force …