Windows Batch: Set Variables from Text File Windows Batch: Set Variables from Text File windows windows

Windows Batch: Set Variables from Text File


Here ya go! Have fun with this one.

(set /p var1=set /p var2=set /p var3=)<Filename.txt

Lands you with the same results!


The FOR /F loop command can be used to read lines from a text file:

@echo offsetlocal ENABLEDELAYEDEXPANSIONset vidx=0for /F "tokens=*" %%A in (sites.txt) do (    SET /A vidx=!vidx! + 1    set var!vidx!=%%A)set var

You end up with:

var1=http://website1.comvar2=http://website2.comvar3=http://website3.com


Based on @Andres' answer, in case anyone is looking only for the values of the variables as an output:

@echo offsetlocal ENABLEDELAYEDEXPANSIONset vidx=0for /F "tokens=*" %%A in (sites.txt) do (    SET /A vidx=!vidx! + 1    set var!vidx!=%%A)for /L %%I in (1,1,%vidx%) do (echo !var%%I!)Pause

output:

http://website1.comhttp://website2.comhttp://website3.com

Of course this method is only helpful if you want to do some text manipulation or something, but it isn't the parctical way if you just want to print the contents of the text file.