Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using robocopy with source and destination as variables

Completely new to scripting, tried to find a solution through searching the web but I'm stumped, so asking for help please!

I'm trying to use robocopy with a variable as both the source and destination, different one for each, but I just can't get the right syntax.

The hard code which I can get to work fine is:

robocopy C:\Users\me\Documents\a C:\Users\me\Documents\b

But I can't get it to work with variables. I've tried, after reading around, what seems like all variations of the below with " and % in/out, with and without Set, and none of them have worked.

Set src="C:\Users\me\Documents\a"
Set dest="C:\Users\me\Documents\b"

robocopy %src% %dest%

Hope that's a clear explanation of what I'm trying to do, if not ask for clarification. Thanks in advance for any help.

like image 308
lcllm7 Avatar asked Nov 13 '13 14:11

lcllm7


People also ask

Does Robocopy create destination folder?

Every robocopy execution will have a source and a destination directory. Robocopy copies and moves files by entire directory.

Does Robocopy overwrite destination files?

Copy files without overwriteRobocopy normally overwrites those. /XN excludes existing files newer than the copy in the source directory. Robocopy normally overwrites those.

What is restartable mode in Robocopy?

Copies files in restartable mode. In restartable mode, should a file copy be interrupted, Robocopy can pick up where it left off rather than recopying the entire file. /b. Copies files in backup mode. Backup mode allows Robocopy to override file and folder permission settings (ACLs).


3 Answers

set "src=C:\Users\me\Documents\a"
set "dest=C:\Users\me\Documents\b"

robocopy "%src%" "%dest%" 

Nothing bad with your sintax. This way is "more robust" or more standard or more habitual, ...

BUT robocopy is not copy not xcopy. You are asking robocopy to copy from source to target changed or new files. If there are no changes, robocopy will not copy anything. If you have tried and it worked, .... if no changes, no file copy.

AND you have not asked robocopy to copy subdirectories. So, if there are no files in source directory, nothing will be copied.

like image 152
MC ND Avatar answered Oct 05 '22 21:10

MC ND


I have found Robocopy is touchy to the point of being arbitrary about syntax. I have found a similar problem to yours:
This code works:

Set Today=%DATE:~0,3%
Robocopy "G:\folder A" "U:\%Today%\folder A"  ^
/S /XJD /R:25 /W:4 /NP /copyall ^
/LOG:"U:\%Today%\FolderALog.txt"
IF ERRORLEVEL 8 goto Badend

This (nicely structured) code doesn't work

Set Today=%DATE:~0,3%
Set source="G:\folder A"
Set target="U:\%Today%\folder A" 
Set Logname="U:\%Today%\FolderALog.txt"
Echo Source is %Source%
Echo Target is %Target%
Echo logfile named %Logname%
Pause
Robocopy %source% %target%   ^  
/S /XJD /R:25 /W:4 /NP /copyall  ^
/LOG:%Logname%
Pause

However, in this 2nd example, take the 1st continuation out of the command line and it works:

Set Today=%DATE:~0,3%
Set source="G:\folder A"
Set target="U:\%Today%\folder A"
Set Logname="U:\%Today%\FolderALog.txt"
Echo Source is %Source%
Echo Target is %Target%
Echo logfile named %Logname%
Pause
Robocopy %source% %target% /S /XJD /R:25 /W:4 /NP /copyall  ^
/LOG:%Logname%
Pause

I've been using the caret (^) as a continuation character in batch command jobs ever since the DOS days, but in this instance the parser tries to concatenate it with the previous variable and the job dies because the system thinks I'm trying to name a folder "U:\%Today%\folder A ^". So goes it -- you keep trying things until something works. Troubleshooting techniques: Doing Echos of newly defined variables then pausing allows you to check for typos and misplaced quote marks. The pause at the end gives you ample time to read the error code, should there be one. Another thing I ran into once was inadvertently inserting an unprintable character in place of a space in a path enclosed with quotes. RoboCopy is very powerful and well worth occasional tinkering with touchy syntax.

like image 29
Just a pilgrim passing thru Avatar answered Oct 05 '22 20:10

Just a pilgrim passing thru


Looks like an old question, but I ran into this issue myself today and solved it by using double slashes for the set command:

Set src="C:\\Users\\me\\Documents\\a"
Set dest="C:\\Users\\me\\Documents\\b"

robocopy %src% %dest%
like image 31
Don Honabach Avatar answered Oct 05 '22 20:10

Don Honabach