Getting second line of text set as a variable using FOR in batch
25,002
try this:
@echo off
for /f "skip=1" %%G IN (1234.txt) DO if not defined line set "line=%%G"
echo %line%
pause
other example:
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" 1234.txt') do if %%G equ 2 echo %%H
Author by
user2654489
Updated on July 09, 2022Comments
-
user2654489 almost 2 years
I have a text file on my desktop named "1234.txt" and it contains four lines of text that looks like:
Test Test1 Test2 Test3
I want to echo the second line (aka Test1) using the FOR command. I am using this:
@echo off for /f "skip=1" %%G IN (1234.txt) DO @echo %%G pause
and it returns
Test1 Test2 Test3 Press any key to continue . . .
How do I set up the FOR command to only read that second line (Test1), not the third and fourth as well? Cheers
-
user2654489 over 10 yearsWorks like a charm, thanks much. Just so I understand how that works, as it's searching through the text document its sets the second line that it reads equal to 'line' (because it's not defined)and when it reads the third line 'line' is already defined so it moves on to PAUSE. Did I get the gist of it?
-
user2654489 over 10 yearsCool, I'll accept your answer in 6 minutes as soon as it will let me. Thanks again!
-
Endoro over 10 yearsIn the second example
findstr
counts the lines. Count is in%%G
, line is in%%H
. Echo it if count=2. -
Albert F D about 9 years@Endoro, your answers have been very useful to me. :) In the first example it appears that if a line contains a space it is only echoed up until the occurrence of the space. How can that be remedied? ...and if I may also ask, I'm trying to understand the second example which works, regardless of spaces. How does the
"tokens=1*delims=:"
and the"^"
parts work in context of the code snippet? -
Albert F D about 9 years@Endoro, the lines of text I'm working with are full path names with spaces. I managed to get your first example working by modifying it with the same options as the second example i.e.
for /f "skip=1 tokens=1* delims=:" %%i in (result.txt) do if not defined line set "line=%%~ni"
echo %line%
...this extracts the trailing folder name from the second line of the result.txt file... but I'm still without understanding how the"tokens=1*delims=:"
part works? -
Endoro about 9 years@Chris The first token "%%G" contains the line number from "findstr /n", the second token "%%H" contains the line itself. The Charet "^" is necessary to read empty lines.
-
Albert F D about 9 years@Endoro ...thanks for clearing that up... but just to be sure, there is nothing special going on, regarding the
tokens
anddelims
...tokens=1*
processes the first token and everything afterwards, anddelims=:
is just that, setting colon as the character for "breaking up" the string, in this case you chose, colon because it's most likely NOT amongst the characters in the string right? (certainly in my case it isn't - which is why I assume it works.) -
Wayfarer almost 8 years@Chris In the first example, to get the whole line instead of getting only the first word, add the
delims
parameter, this way:"delims== skip=1"
It's working for me :-) (Thanks, @Endoro!)