Page 1 of 1
Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 13:18
by J'aitreschaud
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:
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
Re: Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 13:39
by aGerman
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
Re: Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 16:51
by J'aitreschaud
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?

Re: Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 17:13
by ShadowThief
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!
Re: Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 17:20
by aGerman
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
Re: Setting each line of a txt file to a variable is not working
Posted: 23 Jul 2018 18:27
by J'aitreschaud
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.
Re: Setting each line of a txt file to a variable is not working
Posted: 24 Jul 2018 00:36
by aGerman
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
Re: Setting each line of a txt file to a variable is not working
Posted: 24 Jul 2018 14:03
by J'aitreschaud
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
