Author Options:

Batch file set each line in a file as a variable Answered

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!


OK, I have a similar request, I want to do the following:

Text file has a list of usernames




What I want to do is read the first line,

then create a folder D:\users\username1

The apply some permissions to the folder using ICACLS such as

username1 modify; group1 modify; group2 read

Then loop to the next username

then create a folder D:\users\username2

The apply some permissions to the folder using ICACLS such as

username2 modify; group1 modify; group2 read

etc until end of text file then exit


5 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!


Variable names are line_1, line_2 etc.