Access is Denied When Renaming Folder

49.7k Views Asked by At

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.

6

There are 6 best solutions below

3
On BEST ANSWER

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.

1
On

Well before you can run this command you have to have the privileges to do so as overwriting or modifying files from the command line requires admin rights. Once you got that done, enter the following:

ren (path name) (modified name, no quotation marks)
0
On

1 copy path of the folder. To do this, hold shift and right-click, you'll see the "copy as path option"

2 open cmd as admin. To do this, press window + X, then A.

3 in the cmd, type "cd C: then paste the path" so it will be, for example, like:

cd C:\Users\abc\Desktop\music

before you press enter, quote the last name; for example:

cd C:\Users\abc\Desktop\"music"

4 Next, type: ren *.present extension *.new extension; For example, from mp3 to exe:

ren *.mp3 *.exe

If no extension was existing, then, for example:

ren *. *.exe

This should work and eliminate the access denied nuisance.

1
On

I solved this problem by giving the user who run the batch full right on the directory. It seems the right from a group membership is not sufficient: the user was member of the local group Administrators and I received a "Access denied". After having added the user account in the directory security with full rights, it does the job.

1
On

I fixed this by disabling Quick Access:

See Screenshot:

[]

0
On

My approach to solving this problem is to run the utility Handle, by Mark Russinovich of Windows Sysinternals. You can find it here.

This utility will show all the open handles on the system and which process is using them. One of them may have a path to the folder you are trying to rename.

I typically redirect the output of handle.exe to a file.

handle >handles.txt

Then I look at handles.txt in a text editor and search for the name of the folder I was trying to rename. It will appear in a section of handles belonging to the process that is keeping the handle to that folder open, and hence preventing it from being renamed. It shows the pid of the offending process, which you can decide how to deal with, in case you need to terminate it.

e.g. Trying to rename myfolder I forgot I had started a service running using pm2 (a process monitor) The output of Handle.exe included the following:

node.exe pid: 12752 DISCO\Stu
   40: File  (---)   C:\Dev\myfolder\service