Access is Denied When Renaming Folder
Asked Answered
J

7

15

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.

Judaic answered 28/12, 2016 at 15:44 Comment(2)
Supposing there is no other program or the cmd instance itself accessing the folder, you might need to ensure that cmd is properly elevated; see also this article: The Windows Security Model...Abnormal
If you had the appropriate privileges there would be no requirement to modify ownership. The issue is therefore that the privileges under which you are operating are not sufficient.Leomaleon
M
28

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.

Microtone answered 29/12, 2016 at 15:53 Comment(3)
Thanks a lot! Finally got my answer. I tried doing it again and I realized that I did indeed have other things open that were in that folder.Judaic
I had the same problem but with no files open. However I had File Explorer viewing the folders to be renamed and that was locking it.Iridaceous
It's confusing of Windows to say "access denied" in this situation. A better error would be "sharing violation" or "directory or file is in use".Earring
S
3

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
Sigler answered 9/11, 2021 at 21:34 Comment(0)
A
1

I fixed this by disabling Quick Access:

See Screenshot:

[]

Aalst answered 18/5, 2020 at 11:36 Comment(1)
or disabling it by going to windows (10) "Windows Search settings" and add the folder to the exclusions (and maybe restart the computer to take effect (if needed)).Laryssa
F
0

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)
Formosa answered 29/12, 2016 at 2:59 Comment(1)
Thanks, but I already have administrative rights. It must just be a glitch with the windows OS. I even tried to rename it using the move command, but nothing works. I can only do it manually.Judaic
M
0

I encountered an "Access denied" error in a batch file that renames several files in succession, within a certain directory. The batch file was able to rename a few files, but failed with "Access Denied" on some. Oddly, rerunning the same batch file succeeded in renaming a few more files, and rerunning again renamed a few more files. So it was not an outright permissions problem.

It seems that each rename command invokes some additional process that evidently locks the directory for some brief time. But the batch file does not wait for that additional process to complete.

I solved this problem by adding a delay (timeout 5) between each rename command. I don't know the briefest delay that would work, and I'm not sure what program was causing the lock -- perhaps Windows Explorer updating its listing?

Marsh answered 7/5 at 8:38 Comment(0)
U
-1

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.

Unkennel answered 31/5, 2021 at 10:21 Comment(0)
B
-1

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.

Barony answered 4/10, 2021 at 7:30 Comment(1)
This is a different problem. The OP said that he has full administrator access.Pah

© 2022 - 2024 — McMap. All rights reserved.