12

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.

feetwet
  • 3,248
  • 7
  • 46
  • 84
computer_geek64
  • 398
  • 2
  • 3
  • 13
  • Supposing there is no other program or the `cmd` instance itself accessing the folder, you might need to ensure that `cmd` is properly [elevated](http://www.computerhope.com/jargon/e/elevated.htm); see also this article: [The Windows Security Model](https://msdn.microsoft.com/en-us/library/bb385791.aspx)... – aschipfl Dec 28 '16 at 16:01
  • 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. – Compo Dec 28 '16 at 16:08

6 Answers6

26

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    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. – computer_geek64 Dec 31 '16 at 22:00
  • 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. – SingleStepper Mar 30 '17 at 14:00
  • 9
    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". – Carlos A. Ibarra May 17 '17 at 04:20
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
Wyck
  • 10,311
  • 6
  • 39
  • 60
1

I fixed this by disabling Quick Access:

See Screenshot:

[]

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
P Wylie
  • 39
  • 7
  • 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)). – Seeker Oct 14 '20 at 16:42
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)
PryroTech
  • 504
  • 1
  • 3
  • 12
  • 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. – computer_geek64 Dec 29 '16 at 14:36
-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.

-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.