zylor
zylor

Reputation: 13

Replacing a specific line in batch and reading and setting what i read as a variable

I wanted to make a menu like this:

mode 59, 300
set line===========================================================
echo %line%
echo         Tool colour menu!
echo %line%
Echo.
echo     Choose your BACKGROUND colour!
echo.
echo         1  =   Blue        
echo         2  =   Green       
echo         3  =   Aqua        
echo         4  =   Red     
echo         5  =   Purple      
echo         6  =   Yellow      
echo         7  =   White
echo         8  =   Gray
echo         9  =   Light Blue
echo         10 =   Light Green
echo         11 =   Light Aqua
echo         12 =   Light Red
echo         13 =   Light Purple
echo         14 =   Light Yellow
echo         15 =   Bright White
echo.
set /p background_app=Enter the number of the colour that you want for background (or enter for default):

So that menu would be the background_app variable!

And then:

mode 59, 300
set line===========================================================
echo %line%
echo         Tool color menu!
echo %line%
Echo.
echo     Choose your TEXT color!
echo.
echo         1  =   Blue        
echo         2  =   Green       
echo         3  =   Aqua        
echo         4  =   Red     
echo         5  =   Purple      
echo         6  =   Yellow      
echo         7  =   White
echo         8  =   Gray
echo         9  =   Light Blue
echo         10 =   Light Green
echo         11 =   Light Aqua
echo         12 =   Light Red
echo         13 =   Light Purple
echo         14 =   Light Yellow
echo         15 =   Bright White
echo.
set /p text_app=Enter the number of the color that you want for text (or enter for default):

This would be the variable %text_app%

After user input i wanted to save this variables in a txt file so i could retrieve the values later just in case the user runs the tool (to keep the colours that the person is choosing)

But currently i have tried:

:saveVars
(
ECHO Backuground=%background_app%
ECHO Text=%text_app%
) >colors.txt
GOTO :EOF

For example it will save like this:

Background=1
Text=7

And now comes the dilemma, because i wanted to read only the value from colors.txt and set it as variables as:

%background_apptxt%
%text_apptxt%

How can i read only value of Background and Text? Thanks for your help :)


Well it's pretty easy to understand the FOR but for example:

Colors.txt contains this lines:

Background=1
Text=2

So for example i used this batch create a test batch:

@echo off
set background_app=black
set text_app=green

:saveVars
(
ECHO Background=%background_app%
ECHO Text=%text_app%
) >colors.txt

for /f "tokens=1,2 delims==" %%A in (colors.txt) do set "%%A_app=%%B"

How do i echo the %background_app% and echo again %text_app% ? Thank you

Upvotes: 1

Views: 315

Answers (2)

zylor
zylor

Reputation: 13

Well found an alternative to save and read from ini:

http://www.horstmuc.de/wbat32.htm#inifile

Hope someone finds this as usefull as i do :)

Upvotes: 0

dbenham
dbenham

Reputation: 130869

Use a FOR /F loop to read and parse each line. You can set the token delimiter to = so you get 2 tokens.

for /f "tokens=1,2 delims==" %%A in (colors.txt) do set "%%A_app=%%B"

The code could be even easier if your text file contains the full name of each variable. Then you could simply use

for /f "delims=" %%A in (colors.txt) do set "%%A"

Upvotes: 1

Related Questions