使用另一个批处理批处理文件中的变量值

时间:2021-05-06 23:30:45

existing A.bat :

现有的A.bat:

rem set variable pathVariable to appropriate value

set pathVariable=../Doc/English/MyDoc.htm

rem asd

from B.bat, I need to update the value of pathVariable with a path containing spaces, but after replacing spaces with %20 So after running B.bat, A.bat should look like :

来自B.bat,我需要用包含空格的路径更新pathVariable的值,但是在用%20替换空格之后所以在运行B.bat之后,A.bat应该如下所示:

rem set variable pathVariable to appropriate value 

set pathVariable=C:/Program%20Files/XXX%20YYY/MyDoc.htm 

rem asd 

I tried to play with this and could write

我试着玩这个并且可以写

@echo off
setlocal enabledelayedexpansion
set inputFile=A.bat
set outputFile=A_New.bat
set pathVar=../Doc/English/MyDoc.htm
set newPathVarSpace=C:/Program Files/XXX YYY/MyDoc.htm

set space=%%20
set newPathVarNoSpace=%newPathVarSpace: =!space!%
echo %newPathVarNoSpace%

for /f "tokens=1,* delims=" %%A in ( '"type %inputFile%"') do (
SET line=%%A
rem SET modified=!string:%pathVar%=%newPathVarSpace%!
SET modifiedLine=!line:%pathVar%=%newPathVarNoSpace%!

echo !modifiedLine! >> %outputFile%
)

The script works but its eating all the blank lines which I want to preserve. Thanks for your help.

该脚本有效,但它吃掉了我想要保留的所有空白行。谢谢你的帮助。

1 个解决方案

#1


Try below piece of code -

尝试以下代码 -

A.BAT

rem set variable pathVariable to appropriate value.
pathVariable=C:\Test

B.BAT

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "config=.\A.bat"
    set "path=C:\Batch projects\Batch tests\bin\Debug App"

    for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
    ) do for /f "tokens=1 delims== " %%a in ("%%~l"
    ) do if /i "%%~a"=="pathVariable" (
        >>"%config%" echo(pathVariable=%path%
    ) else (
        >>"%config%" echo(%%l
    )

    type "%config%"

    endlocal

Now go to A.BAT, Open it with Notepad and check the pathVariable Value as below -

现在转到A.BAT,用记事本打开它并检查pathVariable Value如下 -

C:\Batch projects\Batch tests\bin\Debug App

#1


Try below piece of code -

尝试以下代码 -

A.BAT

rem set variable pathVariable to appropriate value.
pathVariable=C:\Test

B.BAT

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "config=.\A.bat"
    set "path=C:\Batch projects\Batch tests\bin\Debug App"

    for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
    ) do for /f "tokens=1 delims== " %%a in ("%%~l"
    ) do if /i "%%~a"=="pathVariable" (
        >>"%config%" echo(pathVariable=%path%
    ) else (
        >>"%config%" echo(%%l
    )

    type "%config%"

    endlocal

Now go to A.BAT, Open it with Notepad and check the pathVariable Value as below -

现在转到A.BAT,用记事本打开它并检查pathVariable Value如下 -

C:\Batch projects\Batch tests\bin\Debug App