Access is Denied When Renaming Folder

computer_geek64 picture computer_geek64 · Dec 28, 2016 · Viewed 27.7k times · Source

I don't know if this is the right place to ask this question, but I am going to ask it anyway.

I have a frequent problem when I try to rename one of my folders; it says access is denied. I have full administrative rights on my computer. The problem occurs only when I try to do it via command prompt or batch files. I can manually rename the folder with no problems whatsoever. But I need to be able to rename it using the ren command. It is an irritating problem, and I have heard other people having the same problems. They usually fix it by re-taking ownership of the folder and restarting the computer. This works for me, but I hate having to do this every time. Is there a permanent solution that I can do to stop this problem? Also, when this problem occurs, it happens to all folders; I can't rename any of them using the ren command.

Answer

Mofi picture Mofi · Dec 29, 2016

The answer is quite simple:

Windows does not permit deletion or renaming a directory/folder which is

  • the current directory of any running process (application/service) like the command process, or
  • any subdirectory of the directory to rename is the current directory of any running process, or
  • any file is opened in the directory to rename or any of its subdirectories by any process with OF_SHARE_DENY_READ, OF_SHARE_DENY_WRITE, OF_SHARE_EXCLUSIVE used on opening the file.

In other words as long as the directory itself or any file or subdirectory in this directory or its subdirectories is in use by any application, Windows denies renaming or deleting the directory.

On Unix/Linux it is possible to delete or rename a directory while it is in use by 1 or more running processes. The running *nix process has to handle the special use case that the directory or file just accessed successfully a millisecond before does suddenly not exist anymore.