8

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?

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

5 Answers 5

5

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.

1
  • 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, 2011 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.

5
  • 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, 2011 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 Sep 22, 2011 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, 2011 at 7:28
  • I made new post with more information superuser.com/questions/339067
    – Aziz
    Sep 23, 2011 at 12:09
  • I was in the directory I was trying to move (dir in use) May 11, 2016 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%)
0

Another way is, update the destination content if exist, else, move

PS: add or remove robocopy parameters of your choice

SET ORIGIN=%HOMEDRIVE%\myfolder_origin
SET DEST=%HOMEDRIVE%\myfolder_dest
IF EXIST %DEST% (robocopy %ORIGIN% %DEST% /E /COPYALL /PURGE /MIR /IS /IT /TEE /FFT /ETA /R:10 /W:5 /ZB /V /LOG:"%HOMEPATH%\Desktop\log.txt") ELSE (MOVE /Y %ORIGIN% %DEST%)

robocopy help

robocopy /?
/E :: Copy subdirectories, including empty ones.
/COPYALL :: copy all file information
/PURGE :: remove destination files and directories that no longer exist in the source.
/MIR :: mirror a directory tree
/IS :: Include equal files.
/IT :: Include modified files
/TEE :: Send output to console window and log file.
/FFT :: assume FAT file times (granularity of 2 seconds).
/ETA :: Show estimated time of arrival of copied files.
/R:n :: Number of retries on copies with errors; default value: 1 million.
/W:n :: Wait time between retries; default value: 30 seconds.
/ZB :: Use bootable mode; if access is denied, use backup mode.
/V :: Produce verbose output, including skipped files.
/LOG:file :: Include status in LOG file (overwrite existing log).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .