Angus Comber
Angus Comber

Reputation: 9708

Windows batch file to extract env variables from text file

If I have a text file like this:

var1=3
var2=7
var3=Apple

Would it be possible in a Windows batch file (nothing fancy like PowerShell, it has to run on any Windows NT based PC) to iterate thru this list and generate an env variable (only needs to be accessble by script) so I can do this sort of thing:

for each var in vars/text file
    populate the vars

echo %var1%
echo %var2%
echo %var3%

Upvotes: 2

Views: 143

Answers (2)

David Ruhmann
David Ruhmann

Reputation: 11367

@echo off
setlocal
for /f "delims=" %%A in (file.txt) do set "%%~A"

echo ...
endlocal

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 7095

Like this:

@echo off
setlocal

for /f %%a in (varfile.txt) do set %%a

echo %var1%
echo %var2%
echo %var3%

Upvotes: 2

Related Questions