How can I avoid Access Denied error when renaming a folder.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How can I avoid Access Denied error when renaming a folder.

#1 Post by alan_b » 10 Oct 2011 15:23

I can rename the Mozilla\Firefox profile in my Admin account with password and Admin Authority,
but NOT in the User account that anyone can use with no password.
I am using Windows 7 Ultimate and UAC is always disabled.

All the following was done by CMD.EXE launched by Windows RUN with ADMIN authority
just incase that is a higher level than my own Admin authority which also failed.
Comodo Security had A.V. and Defense+ disabled (and Firewall BLOCKED)
All Windows Explorer screens were closed, and Firefox was closed.
All instances of CMD.EXE were closed apart from the one in use at "C:\Users\Alan>"

Code: Select all

C:\Users\Alan>RENAME "C:\Users\Alan\AppData\Roaming\Mozilla" Mozilla_L

C:\Users\Alan>RENAME "C:\Users\User\AppData\Roaming\Mozilla" Mozilla_L
Access is denied.

C:\Users\Alan>ATTRIB "C:\Users\Alan\AppData\Roaming\Mozilla_L"
        I    C:\Users\Alan\AppData\Roaming\Mozilla_L

C:\Users\Alan>ATTRIB "C:\Users\User\AppData\Roaming\Mozilla"
        I    C:\Users\User\AppData\Roaming\Mozilla

C:\Users\Alan>cacls "C:\Users\Alan\AppData\Roaming\Mozilla_L"
C:\Users\Alan\AppData\Roaming\Mozilla_L NT AUTHORITY\SYSTEM:(OI)(CI)F
                                        BUILTIN\Administrators:(OI)(CI)F
                                        Alan-Desktop\Alan:(OI)(CI)F

C:\Users\Alan>cacls "C:\Users\User\AppData\Roaming\Mozilla"
C:\Users\User\AppData\Roaming\Mozilla NT AUTHORITY\SYSTEM:(OI)(CI)F
                                      BUILTIN\Administrators:(OI)(CI)F
                                      Alan-Desktop\User:(OI)(CI)F

C:\Users\Alan>icacls "C:\Users\User\AppData\Roaming\Mozilla"
C:\Users\User\AppData\Roaming\Mozilla NT AUTHORITY\SYSTEM:(I)(OI)(CI)(F)
                                      BUILTIN\Administrators:(I)(OI)(CI)(F)
                                      Alan-Desktop\User:(I)(OI)(CI)(F)

Successfully processed 1 files; Failed processing 0 files

As you can see, "ATTRIB" and "CACLS" cannot distinguish between the ACL's of the two folders
but "ICACLS" did unknown processing on an un-named file.
I only expected more verbose diagnostics than CACLS - I was not expecting any action.
I was pleasantly surprised to find that allowed success instead of telling me why I was blocked.

I tried again

Code: Select all

C:\Users\Alan>RENAME "C:\Users\User\AppData\Roaming\Mozilla" Mozilla_L

C:\Users\Alan>dir "C:\Users\User\AppData\Roaming\Mozilla_l"
 Volume in drive C is C_System_C
 Volume Serial Number is 3E16-BCE7

 Directory of C:\Users\User\AppData\Roaming\Mozilla_l

04/02/2011  20:17    <DIR>          .
04/02/2011  20:17    <DIR>          ..
04/02/2011  20:17    <DIR>          Extensions
04/02/2011  20:17    <DIR>          Firefox
               0 File(s)              0 bytes
               4 Dir(s)   9,499,660,288 bytes free


This is the original script that worked on my "Alan" profile, and failed on the "User" profile.

Code: Select all

CALL :D_LINK "C:\Users\Alan\AppData\Roaming" "Mozilla" Alan
CALL :D_LINK "C:\Users\User\AppData\Roaming" "Mozilla" User
GOTO :EOF

:D_LINK
SET LINK=%1\%2
SET TARGET=D:\Junctions\%2_%3
XCOPY %LINK% %TARGET% /E /I

REM NEED A FIXIT HERE PERHAPS

RENAME %LINK% %2_L
MKLINK /J %LINK% %TARGET%
pause
GOTO :EOF


I believe ICACLS could be a suitable "FIXIT" but I do not know how it does it nor when it will destroy me.

NB I have used the above script previously with different arguments following "CALL :D_LINK arg1 arg2 arg3"
and it always worked successfully.

Its purpose is to duplicate a designated folder plus contents to a separate partition with reduced frequency of backing up,
and to replace the original with a Junction after renaming the original incase the Junction does not work.

I would like to fix this so my improved script can be successfully adapted to any future requirement.

Is there a better command than "RENAME" overcome this sort of problem ?

I would like to know what it was that caused this "Access is denied" error.

Regards
Alan

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: How can I avoid Access Denied error when renaming a fold

#2 Post by Rileyh » 12 Oct 2011 01:15

alan_b,
The "rename" command is possibly what is causing the access error. You could attempt to use the "runas" command to make it run in an administrator environment. Just as long as you know the password. Also, the error could be because the folder may be a system folder and that means that it cannot be touched. Try those things and post a reply if they don't work.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How can I avoid Access Denied error when renaming a fold

#3 Post by alan_b » 12 Oct 2011 03:09

Thanks for the effort.

I am using Windows 7 with UAC disabled.
I tried Windows Start => RUN... and was told
"This command will run with Administrative Privileges"
and with that authority I ran CMD.EXE - but no access.
Neither the folder I renamed, nor the one that failed, were System.

Before I first posted this problem I knew that any instance of CMD.EXE will possess its own Current Drive:\Path
and Windows will not allow any change to any name on that path whilst CMD.EXE is sitting on it.
I also suspected that Firefox might be possessive of its own profile(s) so I closed Firefox
(even though it had never used that profile since I booted up that day)
Similarly I suspected Windows might object to a change to any path affect a folder that Windows Explorer was looking in,
so I closed all instances of Windows Explorer as well - and it still failed.
The only thing I failed to do was reboot and try again,
but trying to do that under the control of a simple BAT script is a whole new world of pain :twisted:

I have now found that if one instance of Windows Explorer is showing the contents of
"C:\Users\User\AppData\Roaming\"
it can see the name (but not the contents) of the folder "Mozilla" and CMD.EXE is not allowed to change that name.
Although CMD.EXE cannot change the name, Windows Explorer is able to change it,
and then CMD.EXE can see the new name BUT cannot change from that new name to something else - until I close Windows Explorer.

When Firefox is closed a Firefox Process lingers for a bit until its add-ons have finished doing their things
(like Xmarks and LastPass updating to backup servers)
and until that is completed (or task Manager is used to force termination) then CCleaner cannot clean Firefox.

I am now working on the assumption that perhaps Windows Explorer may leave a process running when its window has been closed,
and that could have been the obstacle.

I now know that if my script should fail in the future I need to reboot and try again.

Regards
Alan

Post Reply