How we can use Azure DevOps server 2019's Predefined variable while using ROBOCOPY in bat script

1.1k Views Asked by At

I have created one batch script, in this script I want to use Build.SourcesDirectory variable.

Here is the batch script:

ROBOCOPY $(Build.SourcesDirectory)\myfoldername \\servername\destinationfolder\Copy /V

Also tried this one:

ROBOCOPY "$(Build.SourcesDirectory)\myfoldername" "\\servername\destinationfolder\Copy" /V

but getting error and it's not taking the path as well

Here is my pipeline output

ROBOCOPY::     Robust File Copy for Windows                              

Source : E:\DevOps\Agent\vsts-agent-win-x64-2.144.2\_work\6\s\$(Build.SourcesDirectory)\myfoldername
Dest : \\servername\destinationfolder\Copy

 Files : *.*

Options : *.* /V /DCOPY:DA /COPY:DAT /R:1000000 /W:30

Note: I know there is one task "Windows Machine File Copy task"

I have one bat file which is performed many tasks and one of the tasks is this.

Anyone has an idea, how we can achieve?

2

There are 2 best solutions below

0
On BEST ANSWER

This issue usually occurs when the variable is not recognized. On UNIX systems (MacOS and Linux), environment variables have the format $NAME. On Windows, the format is %NAME% for batch and $env:NAME in PowerShell.

System and user-defined variables also get injected as environment variables for your platform. When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable name any.variable becomes the variable name $ANY_VARIABLE.

Check the following example of a Batch Script using variable in pipeline:

@echo off
echo BUILD_SOURCESDIRECTORY contents:
@dir %BUILD_SOURCESDIRECTORY%
echo Over and out.
1
On

You are reinventing the wheel. Use the Windows Machine File Copy task (for Windows) or the Copy Files over SSH task for non-Windows machines.