繁体   English   中英

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

[英]batch variable nested into a batch variable

根据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

我认为问题出在'SET mypath =%mypath:\\%1 \\ = foo%'中的参数%1,但我不明白为什么。

实际上,我绝对需要使用参数%1,因为当前位于mypath变量中的“仪表板”文本不是静态文本。 它可以是“ Dboard”或其他任何内容

有人可以解释我吗? 先感谢您

对于使用变量的字符串操作,您需要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

劳驾。 有时我认为我不太了解这些问题(可能是因为我的母语不是英语)。

在您的问题中,您说过要替换“在mypath变量中的'X'之后和'Open file'之前的文本...用'foo'替换”,甚至您说“当前在“ myash”变量中的“仪表板”文本不是可以是“ Dboard”,也可以是任何文本”,但是在代码和答案中的后注中,您似乎只是想通过“ foo”更改字符串“ Dashboard”或“ Dboard”。 这使我感到困惑。

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

@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

...正好可以解决您的要求。

        @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

你可以试试这个吗?

您的问题是使用%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%

如果有更优雅的方法,请告诉我们:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM