在AzureDevops批处理文件中当一个变量中有空格时如何连接两个变量

问题描述

我的ps脚本中有此输入。我正在传递%1系统变量来获取输入E:\Build Agents\Agent2\_work\15\a中的路径。该路径具有空格,即set BuildDrop=%1 set Directory=%BuildDrop% + "\adapters\bin" 。我需要将其与其他路径连接起来,但我无法做到这一点。

Directory

这是我的输出,不正确,因为E:\Build Agents\Agent2\_work\15\a\adapters\bin应该类似于set BuildDrop="E:\Build Agents\Agent2\_work\15\a" set Directory="E:\Build Agents\Agent2\_work\15\a" + "\adapters\bin" 。该如何解决

Task         : Batch script
Description  : Run a Windows command or batch script and optionally allow it to change the environment
Version      : 1.1.10

我的任务在我的构建管道中是这样的

{{1}}

解决方法

我找到了解决方案。由于我的%1有空格,因此我需要在分配前删除撇号。我使用~变量来做到这一点。工作代码如下所示。

set "BuildDrop=%~1"
set "Directory=%BuildDrop%\adapters\bin"
,

我们可以在Azure DevOps中使用powershell任务来连接两个变量。 样本:

ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3,found ndim=2. Full shape received: [None,39436]

结果: enter image description here

此外,我找到了similar issue,也请检查它。

更新1

在Azure DevOps管道中使用Power Shell脚本

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

结果:

enter image description here

使用本地电源外壳

结果:

enter image description here

Update2

.bat文件

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

管道结果:

enter image description here

,

您可以使用它来创建包含您的路径的Azure DevOps变量


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


,以及是否需要在powershell脚本中设置变量

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

Here,您有一篇文章如何在Powershell中使用参数。

,

您可以尝试以下方法:

item

示例输出

enter image description here

这是连接代码。

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

在上面的示例中,我尝试使用批处理脚本连接 C:\ Users \ svijay \ Desktop \ adapters \ bin

输出:C:\ Users \ svijay \ Desktop \ adapters \ bin

更新:

%BuildDrop%\adapters\bin

如果有空间,可以提供“”来提供输入。 在您的代码中,您可以删除双引号。

%Directory:“ =%-从字符串文字中删除双引号。

示例输出:

enter image description here