1

I need to get every line in my text in its own variable. Like this:

The text file:

TEMPLATE: Permission, Username, Password;
Admin, Admin, Superflip;
User, Mom, Hi;

I want every line in this file in its OWN variable. Is it possible?

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Kasper H
  • 13
  • 4

2 Answers2

4

The following should work...

@echo off & setlocal enabledelayedexpansion
set num=0
::Change "File_Path" to where your file is. If it is in the same directory, just put the name.
for /f "delims=" %%i in (File_Path) do (
    set /a num+=1
    set line[!num!]=%%i
)

How the script works: The variable num is set for use in the for loop. The for loop goes through each line in the file File_Path setting the line as line, suffixed by a number.

This script emulates creating an array. To call a specific line, put %line[number_of_line]%. For instance, to check if line 3 and line 5 are the same, you would put something like

if %line[3]%==%line[5]% echo Line 3 and 5 are the same.
Aacini
  • 65,180
  • 12
  • 72
  • 108
BDM
  • 3,760
  • 3
  • 19
  • 27
  • +1, If you change it to `for /f "delims=" %%i`, then the complete lines are stored – jeb Feb 09 '13 at 11:15
  • +1, also you can get them all later with `for /f "tokens=1,2 delims==" %%a in ('set line') do echo Line %%a was %%b` – Patrick Meinecke Feb 09 '13 at 22:24
  • 1
    I suggest you to use the standard array notation in these cases: `set line[!num!]=%%i`. See http://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981 – Aacini Feb 10 '13 at 03:25
1

You could also just do this simply:

< filename.txt (
set /p line1=
set /p line2=
set /p line3=
)

That is a much simpler way of doing it.

TrevorPeyton
  • 629
  • 3
  • 10
  • 22