批处理变量嵌套到批处理变量中

问题描述 投票:0回答:4

根据 mypath 变量中“X”之后和“打开文件”之前的文本,我想调用一个将此文本作为参数传递的函数,以便将其替换为“foo”。为此,我写了

@echo off
SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file
if /i %1=="Dashboard" goto label %1
if /i %1=="Dboard" goto label %1

:label 
SET mypath=%mypath:\%1\=foo%
ECHO %mypath%

我注意到在该脚本输出的末尾回显了 mypath

Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file  instead of 
Y:\SUT\ISTQB\Board Airport\X\foo\Open file

我认为问题在于

%1
内的参数
SET mypath=%mypath:\%1\=foo%
,但我不明白为什么。

事实上,我绝对需要使用参数

%1
,因为当前“仪表板”到
mypath
变量中的文本不是静态文本。它可以是“Dboard”,或任何东西

有人可以解释一下吗?先谢谢你了

variables batch-file nested
4个回答
4
投票

对于使用变量进行字符串操作,您需要

delayed expansion

@echo off
SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file
if /i %1=="Dashboard" goto label %1
if /i %1=="Dboard" goto label %1

:label 
setlocal enabledelayedexpansion
SET mypath=!mypath:\%~1\=foo!
ECHO %mypath%

..输出是:

Y:\SUT\ISTQB\Board Airport\XfooOpen file

3
投票

对不起。有时我觉得我不太明白这些问题(也许因为我的母语不是英语)。

在您的问题中,您说您想将“mypath 变量中‘X’之后和‘打开文件’之前的文本替换为‘foo’”,甚至您说“当前 mypath 变量中的“仪表板”文本是不是静态文本。它可以是“Dboard”或任何东西”,但在您的代码和答案中的后面注释中,您似乎只想将字符串“Dashboard”或“Dboard”更改为“foo”。这让我很困惑。

无论如何,这是我对你的问题的解决方案(而不是你的评论)。

@echo off
setlocal EnableDelayedExpansion

SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

rem Get the text after 'X' and before 'Open file'
set auxPath=%mypath%
set enclosed=
set text=
:nextPart
   for /F "tokens=1* delims=\" %%a in ("%auxPath%") do (
      if "%%a" equ "Open file" (set enclosed=) else (
      if defined enclosed (set "text=!text!\%%a") else (
      if "%%a" equ "X" (set enclosed=1)
      ))
      set "auxPath=%%b"
   )
if defined auxPath goto nextPart
set text=%text:~1%

rem Replace that text by "foo"
call :Change "%text%"
goto :EOF

:Change
SET mypath=!mypath:\%~1\=\foo\!
ECHO %mypath%
exit /B

例如,前面程序的输出是这样的:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file

但是如果 mypath 变量是这样的:

Y:\SUT\ISTQB\Board Airport\X\One\Two three\Open file

...输出将是这样的:

Y:\SUT\ISTQB\Board Airport\X\foo\Open file

...正好解决了您的要求。


2
投票
        @echo off
        SET mypath=Y:\SUT\ISTQB\Board Airport\X\Dashboard\Open file

        if /i "%~1"=="Dashboard" goto :label 
        if /i "%~1"=="Dboard" goto :label 

        goto :eof
        :label
        rem set temp_var=%~1
        rem call SET mypath=%%mypath:%temp_var%=foo%%
        rem echo %mypath%
   setlocal enabledelayedexpansion 
        set mypath=!mypath:%~1=foo!
        ECHO !mypath!
    endlocal

你能试试这个吗?


2
投票

您的问题是使用

%1
进行搜索/替换:-
SET mypath=%mypath:\%1\=foo%
有太多
%
字符,有点扰乱语法。

这个答案是使用

EnableDelayedExpansion
来利用
!
语法,但要注意 %mypath% 变量在
endlocal
之后将失去其值 - 它仅将其值保留在
setlocal
块内。

解决此问题的一种方法是将

SET
命令写入临时批处理文件,并在本地块之后调用它:

setlocal EnableDelayedExpansion
  SET mypath=!mypath:%1=foo!
     rem  Create a batch file to perform SET later : 
  echo @SET mypath=%mypath% > "%TEMP%\setmyvar.bat"
endlocal

   rem  Now use the batch file to set the variable :
call "%TEMP%\setmyvar.bat"
ECHO %mypath%

如果有更优雅的方法来做到这一点,请告诉我们:)

© www.soinside.com 2019 - 2024. All rights reserved.