Skip to content

Instantly share code, notes, and snippets.

@sergiorykov
Last active February 13, 2017 13:43
Show Gist options
  • Save sergiorykov/8a0d55febe4ea9a9b988eefbc5073f94 to your computer and use it in GitHub Desktop.
Save sergiorykov/8a0d55febe4ea9a9b988eefbc5073f94 to your computer and use it in GitHub Desktop.
Windows Batch reads simple.ini and sets environment variables
keya=valuea
keyb=%keya%-withb
[ENV] 'keya' = 'valuea'
[ENV] 'keyb' = 'valuea-withb' (expanded from '%keya%-withb')
@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