0

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 
6
  • You are basically parsing the BAT file and interpreting it like the command line processor? And implement that in Batch? Wow! I thought I do crazy stuff sometimes, but I never imagined this. Commented Jan 19, 2022 at 6:30
  • It semms crazy, Do you have any other ideas? Commented Jan 19, 2022 at 6:33
  • You have to run env.bat at some point in the same instance of the command prompt that copy_env.bat is running in for %a% and %b% to actually exist. Stick a call env.bat at the top of copy_env.bat and also fix your third line in env.bat so that you're setting %b% correctly. Commented Jan 19, 2022 at 6:37
  • Other ideas: Run the batch file to actually set the environment variables. Or use a programming language like C++, C#, Python, Java or anything that can do real programming with string replacement etc. Commented Jan 19, 2022 at 6:42
  • @SomethingDark thank you for your answer , but I've tried that It's null eg: dev_env="" Commented Jan 19, 2022 at 6:45

2 Answers 2

2

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 ) 
Sign up to request clarification or add additional context in comments.

5 Comments

I am pretty sure it can be solved without calling the batch file given enough time and effort.
@ThomasWeller - Sure, but there's no reason not to call it; as the name indicates, it's a script meant to initialize environment variables.
awesome ! it's correct
@SomethingDark sorry i hava a question how to determines that the character is in the text repeat?
@ThomasWeller: See my answer above...
1

Ok. Three points here:

  • First of all, the lines in the env.bat file will not be executed inside the env.bat file itself, but in a posterior execution in the copy_env.bat file, right? For this reason, the values of a and b can not be taken via an "immediate" %a% and %b% expansion, but via a "delayed" !a! and !b! one instead.

  • In second place, in copy_env.bat you have the setlocal enabledelayedexpansion and endlocal commands inside the for /F loop. This means that every value that is set in a for iteration will be lost when such an iteration ends. In order to preserve the values for the next for /F cycles, the setlocal-endlocal commands must be moved outside the for /F loop.

  • Finally, you have this command: echo !key!=!value! that just display a line like a=123, but you have not a command with the real assignment of such a value. It is necessary to add the equivalent set !key!=!value! command in order to store such values for the posterior assignments.

The final working programs are these ones:

env.bat:

@echo off set a=123 set b = 456 set dev_env=!a! set prod_env=!b! 

copy_env.bat:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.