24

I am trying to use robocopy but am unable to make it work because of spaces in the directory names.
I am trying to copy 3 directories: My Documents, My Music and My Pictures to 'C:\test-backup' but want the end result to be
'C:\test-backup\My Documents'
'C:\test-backup\My Music'
'C:\test-backup\My Pictures'

My command does not work:
robocopy C:\Users\Angie C:\test-backup "My Documents" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"

No matter what I do, it’s just not happening. Anybody have any suggestions or tricks?

Shannon Matthews
  • 7,816
  • 4
  • 38
  • 69
Angie
  • 721
  • 2
  • 6
  • 18

8 Answers8

92

After some trial and error and observing the results (in other words, I hacked it), I got it to work.

Quotes ARE required to use a path name with spaces. The trick is there MUST be a space after the path names before the closing quote...like this...

robocopy "C:\Source Path " "C:\Destination Path " /option1 /option2...

This almost seems like a bug and certainly not very intuitive.

Todd K.

Shannon Matthews
  • 7,816
  • 4
  • 38
  • 69
toddk63
  • 1,029
  • 1
  • 7
  • 2
  • 3
    Todd, thanks for that. For clarity: `"C:\source path\ "` (there's a space BEFORE the closing quote – th3byrdm4n Jul 01 '15 at 23:29
  • 3
    Wow, what an extremely unintuitive requirement! Thanks Todd! – Shannon Matthews Nov 03 '15 at 06:49
  • 2
    I see in another answer the cause -- the last backslash is treated as an escape character, so I'm not sure if your example applies to this "space after closing quote" requirement? In any event, thanks for the heads up on this peculiarity (I am not OP, just passer-by). Definitely still seems like a bug more than anything, else Robocopy is really dumb. After all, it isn't confused by the first 'escaped' quote. – dyasta Jul 07 '16 at 01:11
  • Not likely a bug, but definitely a bug, undocumented as well! For whom it might concern "mir option" call in java: "ROBOCOPY " + "\"" + srcpath.replace("/", "\\") + " \"" + " \""+ targetpath.replace("/", "\\") + " \"" + " /MIR" – fl0w Sep 26 '16 at 12:16
  • Thanks very much. It seems like a robocopy parsing design flaw or long standing bug. – dgm Aug 05 '20 at 17:40
  • just a note that these problems are caused by the CMD shell parser, not robocopy. they'll never fix them, I think we are meant to move onto PowerShell – Ben McIntyre Feb 06 '21 at 03:16
  • @BenMcIntyre The problem persists in PowerShell. You either have to to add a space after the last back slash or omit the last back slash at all. – stackprotector Mar 15 '21 at 13:41
  • just a note that these problems are caused by the Powershell shell parser, not robocopy. they'll never fix them, I think we are meant to move onto Unix (MINGW!) – Ben McIntyre Mar 16 '21 at 23:24
30

There's no need to add space before closing quote if path doesn't contain trailing backslash, so following command should work:

robocopy "C:\Source Path" "C:\Destination Path" /option1 /option2...

But, following will not work:

robocopy "C:\Source Path\" "C:\Destination Path\" /option1 /option2...

This is due to the escaping issue that is described here:

The \ escape can cause problems with quoted directory paths that contain a trailing backslash because the closing quote " at the end of the line will be escaped \".

Neven
  • 301
  • 3
  • 5
  • 3
    Thank you for the contextual details behind this behavior. Now it makes some sense, as unintuitive as it may remain. Certainly they could/should have done better than this, programmatically. It's pretty easy to assume what the intent is in most all these cases. After all, it isn't confused by the first 'escaped' quote. – dyasta Jul 07 '16 at 01:13
  • 1
    This saved me quite a bit of command line grief. thank you for sharing! – sheldonhull May 23 '17 at 16:19
11

What's with separating My Documents from C:\test-backup? And why the quotes only around My Documents?

I'm assuming it's a typo, try using robocopy C:\Users\Angie "C:\test-backup\My Documents" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"

[Edit:] Since the syntax the documentation specifies (robocopy <Source> <Destination> [<File>[ ...]]) says File, it might not work with Folders.

You'll have to userobocopy "C:\Users\Angie\My Documents" "C:\test-backup\My Documents" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"

Yatharth Agarwal
  • 3,822
  • 20
  • 51
  • According to [technet](http://technet.microsoft.com/en-us/library/cc733145%28WS.10%29.aspx) the syntax is 'robocopy [[ ...]] []' so in my case, I want to copy the My Documents folder. If I use the code you're showing me, will it not copy **everything** in 'C:\Users\Angie' to 'C:\test-backup\My Documents' instead of just My Documents? – Angie Aug 19 '12 at 16:48
  • YatharthROCK: Yes, I think your edit is what I will have to do. Sorry about the confusion, I had hoped that I could list the three folder names in the command rather than write three separate commands... But that is OK, as long as it works :o) Thank you. – Angie Aug 19 '12 at 17:04
  • 1
    @Angie Did you know have _no_ idea what Robocopy is or what it's about? I only skimmed this stuff from the docs. And the link too, you provided me. Sometimes you're very close to an answer, you just need to search a bit more *:)* – Yatharth Agarwal Sep 17 '12 at 17:32
2
robocopy "C:\Users\Angie\My Documents" "C:\test-backup\My Documents" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"
robocopy "C:\Users\Angie\My Music" "C:\test-backup\My Music" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"
robocopy "C:\Users\Angie\My Pictures" "C:\test-backup\My Pictures" /B /E /R:0 /CREATE /NP /TEE /XJ /LOG+:"CompleteBackupLog.txt"
Green Black
  • 4,940
  • 1
  • 14
  • 29
  • Will that not copy **everything** in 'C:\Users\Angie' and place it in 'C:\test-backup\My Documents'? I only want to copy the three folders mentioned above and thought I could list them in the command. 'robocopy [[ ...]] []' – Angie Aug 19 '12 at 16:51
2

There is a bug in robocopy in interpreting the source name. If you include a back slash at the end of the path to describe a folder it keeps including the string for the source into the rest of the line. ie

robocopy "C:\back up\" %destination% /e Nothing here will go to the destination string

robocopy "C:\back up" %destination% /e but this works

I may be wrong but I think both should work!

hotwire
  • 21
  • 1
1

When you specify the last Directory on the path remove the last .

for example "\server\directory with space\directory with space".

that should do it.

Tekwiz
  • 11
  • 1
-2

If this folder is the first in the command then it won't work with a space in the folder name, so replace the space in the folder name with an underscore.

Duncan Jones
  • 59,308
  • 24
  • 169
  • 227
MikeB
  • 1
-2

You should write brackets only before path: "c:\program files\

eebbesen
  • 4,760
  • 7
  • 46
  • 67