0

I have a problem taking a text file with 2 lines of text inside of it, and converting each line of the file into a separate variable.

I thought I could just repeat the same process for getting one line of text and just add skip=1 next to delims= and change the name of the variable it goes into, this did not work how I planned and I can't figure out another way to do it.

Here is my code:

@echo off
for /f "delims=" %%a in (file.txt) do (
set line1=%%a
)
for /f "skip=1 delims=" %%a in (file.txt) do (
set line2=%%a
)

echo %line1%
echo %line2%
pause

The text inside the file file.txt is this:

This is the Text on line 1
This is the Text on line 2

Instead of what i thought the output was going to be -

This is the Text on line 1
This is the Text on line 2
Press any key to continue...

It was this:

This is the Text on line 2
This is the Text on line 2
Press any key to continue...

It just prints the second line of the text file for both of the echos!

Please respond to this question as me not knowing how to do this is really bugging me

Thanks, Alex

Paul
  • 2,620
  • 2
  • 17
  • 27
Alex
  • 13
  • 2
  • You need to `enabledelayedexpansion` – Gary Sep 22 '15 at 12:45
  • 1
    possible duplicate of [How do I increment a DOS variable in a FOR /F loop?](http://stackoverflow.com/questions/2913231/how-do-i-increment-a-dos-variable-in-a-for-f-loop) – Gary Sep 22 '15 at 12:48
  • 1
    `for /F` command always read _all lines_ in the file. You need to insert a `goto` command after the `set line1=%%a` in order to break the loop. – Aacini Sep 22 '15 at 13:49
  • Have you ever heard of search functionality in SO? there are tons of similar questions already... – aschipfl Sep 22 '15 at 13:56

3 Answers3

1

This is a simple way:

@echo off
< file.txt ( set /P "line1=" & set /P "line2=" )

echo %line1%
echo %line2%

If you want to read more lines, or a variable number of lines, I suggest you to read Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks Aacini, this worked out for me and since in a program i am working on i could just add another & set /P "line3=" and that would work?? i will have to look up Arrays and how they work because i am very new to batch and when i tried other languages i allways thought they were hard so i didnt learn them. Thanks for the working code :) – Alex Sep 22 '15 at 22:33
0

Take a look at this:

vars.txt (file with variables):

var1
var2
var3

tast.bat:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET varFile=vars.txt
SET i=1
FOR /F %%l IN (%varFile%) DO (
    SET line!i!=%%l
    SET /a i=!i!+1
)
ECHO !line1!
ECHO !line3!
PAUSE

Output:

var1
var3

This will work regardless how many lines/variables you have. If you have only one, it will be stored in !line1!, if you have 1000, the 500th line will be stored in !line500! and so on.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
0

This should work for lines with spaces by the help of tokens=*

@echo off

rem Creating env testing
(echo First blank line should be skiped
    for /l %%i in (1,1,30) do ( echo This is the Text on line %%i)
)>_vars.tmp

setlocal enabledelayedexpansion
set i=1
for /F "skip=1 tokens=*" %%a in (_vars.tmp) do (
    set line!i!=%%a
    set /a i+=1
)
echo:!line1!
echo:!line3!
echo:!line30!
echo:!line100!

echo checking _vars.tmp
more _vars.tmp
del _vars.tmp

pause
exit /b 0
Paul
  • 2,620
  • 2
  • 17
  • 27