Setting each line of a txt file to a variable is not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Setting each line of a txt file to a variable is not working

#1 Post by J'aitreschaud » 23 Jul 2018 13:18

Salut tout le monde!
I have a problem with setting each line of a txt file to a variable. All the variables have a blank value, whichever method I try. The txt file (Input.txt)contains this:

Code: Select all

Panda Hello
Icebear Hey
I've tried multiple methods, none of which work like this one:

Code: Select all

setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (Input.txt) do (
  set "Line!Counter!=%%x"
  set /a Counter+=1
)
echo %Line1%
Or this one

Code: Select all

setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("Input.txt") do (
  set /a N += 1
  set "Line!N!=%%A"
)
echo %Line1%
Or even this one I found:

Code: Select all

setlocal EnableDelayedExpansion
<"Input.txt" (
  for /f %%i in ('type "Input.txt" ^| find /c /v ""') do set /a n=%%i && for /l %%j in (1 1 %%i) do (
    set /p "line_%%j="
  )
)
for /l %%i in (1 1 !n!) do echo(!line_%%i!
None of them work. I just want, say, variables for each line of the txt file. Like Line1=Panda Hello or Line2=Icebear Hey

Any help would be awesome, thank you

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Setting each line of a txt file to a variable is not working

#2 Post by aGerman » 23 Jul 2018 13:39

Maybe your text file was saved UTF-16 encoded.
Try using TYPE

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%A in ('type "Input.txt"') do (
  set /a N += 1
  set "Line!N!=%%A"
)

for /l %%i in (1 1 %N%) do echo !Line%%i!
pause
Steffen

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Setting each line of a txt file to a variable is not working

#3 Post by J'aitreschaud » 23 Jul 2018 16:51

Thank you so much AGerman! It worked like a charm. I also have one small question. I have this code:

Code: Select all

setlocal enabledelayedexpansion
set n=1
for /f "tokens=* delims=:@" %%L in ('findstr /bl :::@ "%~f0"') do (
     set Line!n!=%%L
     set /a n+=1
)
pause

Theoretically, it takes each line with ":::@" before it and puts it in a variable. The lines are:

Code: Select all

:::@                   ²±±°°°°°°°°°°°°°°°°°°°±±²   ±Û°Û    ±Û°°                
:::@               ²±°°°°°°°°°°°°°±°±°°°°°°°°°°°°°°Û°±°Û±°°Û°°Û±               
:::@            ²°°°°°°±±±°±°±°±°±°±°±±±°±°±°±°±°°°°°±±°°°°±±±°°°°             
:::@     °°°°±²°Û°°±±°±°±°±±±°±°±°±°±°±±±°±°±±±°±°±±±°±°±°±±±±±±°°Û±           
:::@    °Û°±°Û°°±±°±°±°±°±±±°±°±±±°±°±°±°±°±°±±±±±°±±±°±±±°±±±±±±±°Û°          

However, when I put this:

Code: Select all

FOR /l %%G in (1, 1, %n%) DO (
    echo %Line!n!%
)
It doesn't display the contents. Any ideas? :)

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Setting each line of a txt file to a variable is not working

#4 Post by ShadowThief » 23 Jul 2018 17:13

You can't use % and ! in that order. ! always goes on the outside. Also, %n% is your max number, not the counter.

You should be echoing !Line%%G!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Setting each line of a txt file to a variable is not working

#5 Post by aGerman » 23 Jul 2018 17:20

J'aitreschaud wrote:
23 Jul 2018 16:51
Any ideas? :)
Yes. That's basically the same as your former question. So why didn't you use the same scheme?

Code: Select all

@echo off
setlocal enabledelayedexpansion
set n=0
for /f "tokens=* delims=:@" %%L in ('findstr /bc:":::@" "%~f0"') do (
     set /a n+=1
     set Line!n!=%%L
)

for /l %%G in (1 1 %n%) do echo !Line%%G!
pause

goto :eof
:::@                   ²±±°°°°°°°°°°°°°°°°°°°±±²   ±Û°Û    ±Û°°                
:::@               ²±°°°°°°°°°°°°°±°±°°°°°°°°°°°°°°Û°±°Û±°°Û°°Û±               
:::@            ²°°°°°°±±±°±°±°±°±°±°±±±°±°±°±°±°°°°°±±°°°°±±±°°°°             
:::@     °°°°±²°Û°°±±°±°±°±±±°±°±°±°±°±±±°±°±±±°±°±±±°±°±°±±±±±±°°Û±           
:::@    °Û°±°Û°°±±°±°±°±°±±±°±°±±±°±°±°±°±°±°±±±±±°±±±°±±±°±±±±±±±°Û°          
Steffen

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Setting each line of a txt file to a variable is not working

#6 Post by J'aitreschaud » 23 Jul 2018 18:27

Merci beacoup Shadowthief.
To answer your question aGerman, I didn't really want tons of extra txt files for the stuff I was doing, so I just wanted one file for everything instead.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Setting each line of a txt file to a variable is not working

#7 Post by aGerman » 24 Jul 2018 00:36

That's what I already understood (see my example code). I referred to the way how to process and output the lines which isn't much different to your first code as you might have seen. The reason why I asked is that you for whatever reason used a wrong variable expansion in your FOR /L loop even if that would have been only a copy/paste from the code I already wrote in my first answer.

Steffen

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Setting each line of a txt file to a variable is not working

#8 Post by J'aitreschaud » 24 Jul 2018 14:03

Hehe, I see your point. I think I was just being stupid, and was dumb enough not to realize that it was the method at the core :oops:

Post Reply