Home > database >  How does batch Echo parse variables in text
How does batch Echo parse variables in text

Time:01-19

How to batch echo output value is from another file, and the content is a variable, how to parse it?

example:

env.bat

@echo off
set a=123
set b = 456

set dev_env=%a%
set prod_env=%b%

copy_env.bat

@echo off
for /f "skip=1 eol=: tokens=2,3 delims== " %%i in (.\env.bat) do (
    setlocal enabledelayedexpansion
    set key=%%i
    set value=%%j

    echo !key!=!value! >> .\custom_variable.properties

    endlocal
)

output: this is not what I want

dev_env=%a%
prod_env=%b%

output: I want ....

dev_env=123
prod_env=456

CodePudding user response:

You need to actually call env.bat at some point so that the variables get set. Also, because echo !key!=!value! by itself will display dev_env=%a%, etc. you need to use call for a second pass of variable expansion.

@echo off
setlocal enabledelayedexpansion

call env.bat
for /f "skip=1 eol=: tokens=2,3 delims== " %%i in (.\env.bat) do (
    set key=%%i
    set value=%%j

    call echo !key!=!value! >>.\custom_variable.properties
)
  •  Tags:  
  • Related