Last active
February 13, 2017 13:43
-
-
Save sergiorykov/8a0d55febe4ea9a9b988eefbc5073f94 to your computer and use it in GitHub Desktop.
Windows Batch reads simple.ini and sets environment variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keya=valuea | |
keyb=%keya%-withb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ENV] 'keya' = 'valuea' | |
[ENV] 'keyb' = 'valuea-withb' (expanded from '%keya%-withb') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal EnableDelayedExpansion | |
rem http://stackoverflow.com/questions/25324354/windows-batch-files-what-is-variable-expansion-and-what-does-enabledelayedexpa | |
rem http://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file | |
set file=%~1 | |
for /f "usebackq delims=" %%a in ("!file!") do ( | |
set ln=%%a | |
for /f "tokens=1,2 delims==" %%b in ("!ln!") do ( | |
set currkey=%%b | |
set currval=%%c | |
rem echo '!currkey!' with '!currval!' | |
for /f "delims=" %%a in ('echo !currval!') do ( | |
set "!currkey!=%%a" | |
if "!currval!" == "%%a" ( | |
echo [ENV] '!currkey!' = '%%a' | |
) else ( | |
echo [ENV] '!currkey!' = '%%a' (expanded from '!currval!') | |
) | |
rem echo new value of '!currkey!' is '%%a' | |
) | |
) | |
) | |
endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment