Home > Blockchain >  batch: read env var from file and asign the value prompt
batch: read env var from file and asign the value prompt

Time:10-18

I have a file with a list of variables. I need to grab those variables and ask the user what value they want to assign to each one. And so set those environment variables with that value typed in by the user. I have this.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
cls
set "TEMP="

for /F "tokens=1,2 delims==" %%a in (file.txt) do (
    rem if "!TEMP!"=="" (SET TEMP=%%a) ELSE (SET TEMP=!TEMP!;%%a)
    SET TEMP=%%a
    echo Type %%b :
    set /p "PASS=>"%%b
    echo !TEMP!=!PASS! >> .mpo.txt
    SET /A "TEMP=!PASS!"
)

the file look like this:

VAR1
VAR2
VAR3

the user type: value1, value2, value3 And with the result to set it as envvar should look like this:

VAR1=value1
VAR2=value2
VAR3=value3

of course it doesn't work. any help?


I add an extra for to read from the file: .mpo.txt

for /F "tokens=1,2 delims==" %%f in (.mpo.txt) do (
    setlocal
    SET /A "%%f=%%g"
)

the idea is to set line by line of this file as a envvars

in the for before I do this:

for /F "tokens=1,2 delims==" %%a in (file.txt) do (
    rem if "!TEMP!"=="" (SET TEMP=%%a) ELSE (SET TEMP=!TEMP!;%%a)
    SET TEMP=%%a
    echo Type %%b :
    set /p "PASS=>"%%b
    echo !TEMP!=!PASS! >> .mpo.txt
)
for /F "tokens=1,2 delims==" %%f in (.mpo.txt) do (
        setlocal
        SET /A "%%f=%%g"  rem ANY HELP HERE
    )

Tha way I have inside the file the result that iam looking for.

VAR1=value1
VAR2=value2
VAR3=value3

But how to set it as env var?

CodePudding user response:

Sorry guys, the exact anwser for what I was looking for was this one:

for /F "tokens=1,2 delims==" %%a in (file.txt) do (SET /P %%a="%%b: ")

That way I Set as envvar the variable from the text and the value what the user type as input.

  • Related