Access denied, cmd move windows 7

8

1

I can't use this command in win 7 when want to move a directory if the destination exists. It says Access denied. Why hapens this? It worked in XP.

move /y "%1" c:\mydir\

I can use robocopy, but then it will move only the contents of the folder. not the folder completly.

robocopy "%1" c:\mydir /E /IS /MOVE

How can I solve this problem?

Aziz

Posted 2011-09-22T10:11:50.540

Reputation: 557

When moving files then move command works well. But when source is a folder then it fails if destination alredy exists and it has to overwrite. – Aziz – 2011-09-22T11:13:35.440

Does xcopy work? If so you could use xcopy and then delete the source as a workaround? – None – 2011-09-22T12:07:15.110

2Access denied can also mean "a file in this folder or the folder itself is still opened by a running program". – Robert – 2011-09-22T13:03:28.337

xcopy works well, but robocopy is better since I don't need to delete the copyed directories at the end. But in this case I can only move the contents of source folder since I want to do use %1. this is horrible!! – Aziz – 2011-09-22T13:59:57.790

I made new post with more information http://superuser.com/questions/339067

– Aziz – 2011-09-23T12:09:38.490

Answers

4

At last.. here is the solution.. Thanks for help guys :)

SET mydir=C:\mydir
IF EXIST "%mydir%\%~n1\" (
  ROBOCOPY %1 "%mydir%\%~n1" /E /IS /MOVE
) ELSE (
  MOVE /Y %1 "%mydir%\"
)

Aziz

Posted 2011-09-22T10:11:50.540

Reputation: 557

4

Try:

IF EXIST "c:\mydir" (
     robocopy "%1" c:\mydir /E /IS /MOVE 
     ) ELSE (
     move /y "%1" c:\mydir 
     )

This will check if the folder exists and move contents if the folder exists and if the folder doesn't exits then it will move your folder. If you still get access denied then you probably need to get admin privileges.

MaskedPlant

Posted 2011-09-22T10:11:50.540

Reputation: 408

Thanks for your answer, this was nice try, but i dont need to check if mydir exists.. i need to check if %1 is a directory, then check if a folder withh same name exists in mydir, then do exactly what you do above. check http://superuser.com/questions/339067/cmd-move-cant-replace-directories-in-win-7

– Aziz – 2011-09-27T09:04:13.023

2

If you are getting ACCESS DENIED error messages when attempting to move a folder, either

  1. You do not have correct permissions to move the folder
  2. You do not have the correct permissions to move one or more of the files in the folder
  3. One or more files are being accessed by the system/an application
  4. One or more of the files are protected from deletion.

Check for all of these possibilities.

Mechaflash

Posted 2011-09-22T10:11:50.540

Reputation: 1 226

I was in the directory I was trying to move (dir in use) – Eric Fossum – 2016-05-11T15:36:20.363

1It has to be another thing.. Please Try it if you have win7. Creat a folder tree like this root\aaa\test\a.txt and root\bbb\test\b.txt, then try "move root\aaa\test root\test" and "move root\move\test root\test" first will be ok, but next wil fail – Aziz – 2011-09-22T13:55:37.293

@Aziz, are you asking to MOVE "C:\aaa\test" "C:\test" and THEN MOVE "C:\bbb\test" "C:\test"? I can't test as I don't have Win7, but it doesn't make sense that one would work but not the other. The only other thing that comes to mind is if you have identical filenames in folders aaa and bbb, and attempting to overwrite them is returning ACCESS DENIED – Mechaflash – 2011-09-22T14:22:47.770

MOVE with /Y option worked well in Windows XP. I have not tryed in Vista. The "test" folders in each directory (aaa,bbb,etc..) may have several same files and folders which is why i want to merge those folders.. The operation is same as drag&drop-move each "test" folder into same place an confirm all overwriting. Hovever I can get same result with xcopy or robocopy operation and then delete the source. You can MOVE and replace existing files, but replacing directories gives a "Access is Denied" in win7. I need to solve this, OR i need help to convert the MOVE command as my try in my question. – Aziz – 2011-09-23T07:28:57.440

I made new post with more information http://superuser.com/questions/339067

– Aziz – 2011-09-23T12:09:47.053

0

I'm lame and don't understand exactly what the answers here are doing under the hood so I came up with this copy/paste doozy

SET src=C:\dev
SET dest=D:\dev
IF EXIST %dest% (ROBOCOPY %src% %dest% /E /IS /MOVE) ELSE (MOVE /Y %src% %dest%)

tylerlindell

Posted 2011-09-22T10:11:50.540

Reputation: 141