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?

3
  • Basically you want to create variable arrays? Feb 8, 2013 at 21:16
  • are the number of lines going to be fixed? Feb 8, 2013 at 22:42
  • Don't forget to pick a best answer if you've solved your question.
    – BDM
    Mar 4, 2013 at 8:56

2 Answers 2

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.
3
  • +1, If you change it to for /f "delims=" %%i, then the complete lines are stored
    – jeb
    Feb 9, 2013 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 Feb 9, 2013 at 22:24
  • 1
    I suggest you to use the standard array notation in these cases: set line[!num!]=%%i. See stackoverflow.com/questions/10544646/dir-output-into-bat-array/…
    – Aacini
    Feb 10, 2013 at 3: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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.