loading

Batch file set each line in a file as a variable

I have the following code:
for /F "delims=" %%i in (test.txt) do set content=%content% %%i

This sets the last line of "test.txt" as the %content% variable. My only problem is that I want it to set each line as multiple variables.
FOr example, if "test.txt" has 7 lines of coding or letters and what not, I want my batch file containing the above command to be able to create a new variable for every line there is. Can someone figure this out somehow? Full credit to whoever does! Thank you!

g-one4 years ago
It's unusual to save each line in a separate variable. Normally you would process their contents either directly inside of the loop or you would call a subroutine for each line. Regardless it's not a big deal to create a bundle of new variables.

@echo off
set "file=test.txt"

setlocal EnableDelayedExpansion
<"!file!" (
  for /f %%i in ('type "!file!" ^| find /c /v ""') do set /a n=%%i && for /l %%j in (1 1 %%i) do (
    set /p "line_%%j="
  )
)

echo *** number of lines: !n!
echo *** line contents:
for /l %%i in (1 1 !n!) do echo(!line_%%i!

pause


Variable names are line_1, line_2 etc.