0

I have this input in my ps script. I am passing $(Build.ArtifactStagingDirectory) system variable to get the path in the input %1. The path has spaces i.e. E:\Build Agents\Agent2\_work\15\a. I need to concatenate this with other path but I am not able to do so.

set BuildDrop=%1
set Directory=%BuildDrop% + "\adapters\bin"

This is my output, which is incorrect as Directory should be something like E:\Build Agents\Agent2\_work\15\a\adapters\bin. How to solve this?

set BuildDrop="E:\Build Agents\Agent2\_work\15\a" 

set Directory="E:\Build Agents\Agent2\_work\15\a" + "\adapters\bin" 

My task is like this in my build pipeline

Task         : Batch script
Description  : Run a Windows command or batch script and optionally allow it to change the environment
Version      : 1.1.10
Mehul Parmar
  • 347
  • 4
  • 21

4 Answers4

1

I found the solution for this. Since my %1 had space I needed to remove apostrohphe before assigning. I did it using ~ variable. working code looks like this.

set "BuildDrop=%~1"
set "Directory=%BuildDrop%\adapters\bin"
Mehul Parmar
  • 347
  • 4
  • 21
0

We can use powershell task in the Azure DevOps to concatenate two variables. Sample:

Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)"
Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)\adapters\bin"

Result: enter image description here

In addition, I found a similar issue, please also check it.

Update1

Use power shell script in the Azure DevOps pipeline

$testpath1 = "E:\Build Agents\Agent2\_work\15\a"
$testpath2 = "\adapters\bin"
Write-Host "path is $($testpath1)$($testpath2)"

Result:

enter image description here

Use local power shell

Result:

enter image description here

Update2

.bat file

set testpath1=E:\Build Agents\Agent2\_work\15\a
set testpath2=\adapters\bin
set newpath=%testpath1%%testpath2%
echo %newpath%

Pipeline result:

enter image description here

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • how to set the variable value? Write-host will only print it. – Mehul Parmar Sep 03 '20 at 09:47
  • If you want to use Azure DevOps variable you should use logging commands `Write-Host "##vso[task.setvariable variable=testvar;]testvalue"` https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell – Krzysztof Madej Sep 03 '20 at 10:02
  • Hi @MehulParmar, I have updated the answer, please check it,. – Vito Liu Sep 03 '20 at 10:05
  • @VitoLiu-MSFT yes this will print it. But how to set the value of a variable. i want to change the value of the variable `Directory` – Mehul Parmar Sep 03 '20 at 10:15
  • @MehulParmar, do you mean that you want to change the env variable Build.ArtifactStagingDirectory? Or add a new env variable in the pipeline? If yes, please check this [answer](https://stackoverflow.com/a/63261717/13903626) – Vito Liu Sep 03 '20 at 10:18
  • @VitoLiu-MSFT I want to set the value in my bat file only. – Mehul Parmar Sep 03 '20 at 10:22
  • @MehulParmar, please check the update2, set the value in the bat file and concatenate two variables, then print it. – Vito Liu Sep 03 '20 at 10:51
  • Please also check this [issue](https://stackoverflow.com/questions/7846560/how-concatenate-two-variables-in-batch-script) – Vito Liu Sep 03 '20 at 10:57
  • @VitoLiu-MSFT yes sure I will have a look. Thanks for your help :) – Mehul Parmar Sep 03 '20 at 11:00
0

You may use this to create Azure DevOps variable containing your path


powershell: |
  
  $directory = $(Build.ArtifactStagingDirectory)                                                                                                                                                                                                                                                                                    
  $newDirectory = $directory + "\adapters\bin"
                                        
  Write-Host "##vso[task.setvariable variable=testvar]$newDirectory " 


and if you need set variable in powershell script

$BuildDrop=$args[0]
$Directory=$BuildDrop + "\adapters\bin"

Here you have an article how to use parameters in powershell.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
0

You could try this :

set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
echo %Directory%

Sample Output

enter image description here

This is the concatenation code.

%BuildDrop%\adapters\bin

In my above sample I am trying to concatenate C:\Users\svijay\Desktop & \adapters\bin using the batch script

And the output : C:\Users\svijay\Desktop\adapters\bin

UPDATE :

@echo off 
set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
set Directory= %Directory:"=%
echo %Directory%

If you have space, you could provide the "" to provide input. In your code you could remove the double quotes.

%Directory:"=% - Removes the Double quotes from the string literal.

Sample output :

enter image description here

Satya V
  • 3,811
  • 1
  • 6
  • 9
  • @mehul - the above is for the batch. Pls let me know if this is the one you are looking for ? – Satya V Sep 03 '20 at 10:36
  • yes but the problem is `BuildDrop="E:\Build Agents\Agent2\_work\15\a"` has apostrophe who also get concatenated. That I don't want. I need an apostrophe to read a value that has spaces. – Mehul Parmar Sep 03 '20 at 10:36
  • BuildDrop=E:\Build Agents\Agent2\_work\15\a ...Don't give the apostrophe . Type the string without the apostrpohe. Something like above example – Satya V Sep 03 '20 at 10:37
  • If you closely look in my sample output. I have not given the apostrophe. The output is just C:\Users\svijay\Desktop\adapters\bin – Satya V Sep 03 '20 at 10:38
  • yes because your input doesn't have space. Try with `word.bat C:\Users\s vijay\Desktop`. My input folder has space, therefore, need an apostrophe. – Mehul Parmar Sep 03 '20 at 10:43
  • Got your point. I have updated my answer. Pls check and let me know :) – Satya V Sep 03 '20 at 10:56
  • Yes, it solved the problem. I have added my solution also. Thank you so much! :) – Mehul Parmar Sep 03 '20 at 11:05