1.

Solve : DOS for loop question?

Answer»

I am tryng to use for /f command to read in the file and returning a first line. Is there anyone who KNOWS if I can tell for /f command to give me the first line in the txt file?after echoing the line, exit.if you don't want to exit, you can jump to a label.

for /F "delims==" %%a in (test.txt) do set line=%%a & goto Next
:Next
echo %line%

This works fine as long as the first line does not have any
"poison" characters (", %, ^ etc.).
Or you can call it as a function, and exit on the first loop:
Code: [SELECT]@echo off
call :PrintFirstLine
echo The REST of your code goes here
goto :EOF

:PrintFirstLine
for /f "delims=" %%a in (test.txt) do echo %%a&exit /b
If you want only the first line, use this

Set /P FirstLine=
GrahamQuote from: gpl on May 24, 2007, 09:53:38 AM

If you want only the first line, use this

Set /P FirstLine=<test.txt

Graham

That takes my vote for BEST suggestion. No need for a FOR loop, and all in one line.


Yes, Graham's solution is simple and robust. I like it.I works like a charm. thank you all.

Robtem


Discussion

No Comment Found