6

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?

migrated from stackoverflow.com Sep 22 '11 at 12:25

This question came from our site for professional and enthusiast programmers.

  • 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 Sep 22 '11 at 11:13
  • Does xcopy work? If so you could use xcopy and then delete the source as a workaround? – Bali C Sep 22 '11 at 12:07
  • 1
    Access denied can also mean "a file in this folder or the folder itself is still opened by a running program". – Robert Sep 22 '11 at 13:03
  • 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 Sep 22 '11 at 13:59
  • I made new post with more information superuser.com/questions/339067 – Aziz Sep 23 '11 at 12:09
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.

  • 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 superuser.com/questions/339067/… – Aziz Sep 27 '11 at 9:04
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%\"
)
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.

  • 1
    It 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 Sep 22 '11 at 13:55
  • @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 Sep 22 '11 at 14:22
  • 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 Sep 23 '11 at 7:28
  • I made new post with more information superuser.com/questions/339067 – Aziz Sep 23 '11 at 12:09
  • I was in the directory I was trying to move (dir in use) – Eric Fossum May 11 '16 at 15:36
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%)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.