简体   繁体   中英

Reading values from file and assigning it to variable in batch script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM