3

I want to read the file which contains the value of some variables which are used in my batch script. I have created a property file with format

key=key_value
key=key_value

Now, I want to set environment variable's name as key and its value as key_value How can I assign?

I have read the file but cannot separate the string "key=key_value" into two strings. Thanks in advance.

1 Answer 1

3

With the FOR command you can turn your key/value file from this

KEY1=value
KEY2=value

into this

SET KEY1=value
SET KEY2=value

which you can then invoke as a batch file to set all of the keys as environment variables. this only works if all of the keys are unique, but from your question it sounds like they are.

save this as a batch file

@echo off
echo rem generated from keyvalue.txt > keyvalue.bat
for /F "tokens=*" %%I in (keyvalue.txt) do @echo set %%I >> keyvalue.bat

call keyvalue.bat

This .bat code assumes that your key/value file is keyvalue.txt and that there are no lines other than blank lines or key=value pairs.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to be picky, you don't need a separate batch file: for /F "tokens=*" %%I in (keyvalue.txt) do set %%I will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.