LabRat
LabRat

Reputation: 2014

set variables in batch to text file values

I have a text file which is made up of a new value on each line. The amount of lines will vary (expand over time).

I would like to set a variable in a batch file for each of those values. Does anybody know how to do that?

Upvotes: 1

Views: 2174

Answers (1)

Spencer May
Spencer May

Reputation: 4505

If you just want to read from each line of the file into separate variable then use this. It can also be configured into a loop if you want it to get all the lines instead of just specific lines that way you wouldn't have to put 100 commands for 100 lines.

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (TEXTFILEPATH.txt) do (
set /a N+=1
set v[!N!]=%%a
)
set line1=%v[1]%
set line2=%v[2]%
set line3=%v[3]%
set line4=%v[4]%

echo %line1%
echo %line2%
echo %line3%
echo %line4%

endlocal

Make sure the endlocal is after the use of the variables.

If you want to write to specific lines in the text file, here is a post for that.

Write batch variable into specific line in a text file

Upvotes: 3

Related Questions