7

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
  • 85
  • 1
  • 1
  • 4

2 Answers2

15

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
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Works 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 Aug 06 '13 at 14:52
  • Cool, I'll accept your answer in 6 minutes as soon as it will let me. Thanks again! – user2654489 Aug 06 '13 at 14:53
  • In the second example `findstr` counts the lines. Count is in `%%G`, line is in `%%H`. Echo it if count=2. – Endoro Aug 06 '13 at 14:55
  • @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 Apr 07 '15 at 18:44
  • @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? – Albert F D Apr 08 '15 at 03:31
  • @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. – Endoro Apr 08 '15 at 05:05
  • @Endoro ...thanks for clearing that up... but just to be sure, there is nothing special going on, regarding the `tokens` and `delims`... `tokens=1*` processes the first token and everything afterwards, and `delims=:` 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.) – Albert F D Apr 08 '15 at 20:24
  • 2
    @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!) – Wayfarer May 05 '16 at 11:04
0

working solution only for one line (4)

::get 4 LINE
for /f "skip=3" %%i in (test.txt) do (set 4LINE=%%i & goto suite)
:suite
echo Detected variable : ==^>  !4LINE!
DIMM_V2
  • 105
  • 1
  • 9