10

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.

lcllm7
  • 101
  • 1
  • 1
  • 3
  • What is happening when you run `robocopy %src% %dest%`? Error messages? – aphoria Nov 13 '13 at 14:39
  • 1
    Did you call the batch file `robocopy.bat` ? Explain how it doesn't work... – foxidrive Nov 13 '13 at 14:40
  • The above script is saved as name.bat When it is run the command line opens, closes but the files aren't copied. I don't know how to see the report from the command line but I've glimpsed something about 'file path not found' – lcllm7 Nov 13 '13 at 14:52
  • Open a command prompt, `CD` to the directory your batch file is in, and then run `name.bat`. This will allow you to see the error message. – aphoria Nov 13 '13 at 15:03
  • Did the comments help you, or are you still having issues? – foxidrive Nov 14 '13 at 02:20
  • did you tried ***powershell and robocopy*** ? – Kiquenet Dec 29 '16 at 13:36
  • I have a similar issue, details here: https://stackoverflow.com/questions/63130769/using-variable-in-jenkins-file-for-robocopy-command#63130769 – Vinod Kumar Gupta Jul 28 '20 at 09:51

6 Answers6

4
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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
4

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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
2

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%
slfan
  • 8,950
  • 115
  • 65
  • 78
0

If you are trying to use a .bat the code will be like this:

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

robocopy.exe %src% %dest%

You forgot to put the .exe

derHugo
  • 83,094
  • 9
  • 75
  • 115
0

Try this way

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

robocopy %src% %dest% /E

/E - for copying of Subfolder including empty subfolder

-3

dude try the XCOPY command it will work for sure!

example:

xcopy "C:\Users\me\Documents\a" "C:\Users\me\Documents\b"

it will ask is the destination is a folder or a file, so once u chose folder (dir) it will copy!

hope it helps! :D

QbicSquid
  • 165
  • 1
  • 5