new
new

Reputation: 1159

Get the first item of a file delimited by a character

I need to be able to extract the line number of the first line of a file. I don't care of others lines

  for /f "tokens=1* delims=:" %%a in (myfile.txt) do set line=%%a
  echo.%line%

  myfile.txt
  7:I=output7
  515:I=output515
  837:I=output837
  851:I=output851

My code obviously retrieves the last line number '851'. I want '7' to be retrieved. I'd not like to use delayed expansion. Please how could I do that elegantly?

Is it possible to cut down a file from down to the top as one could cut a file from top to down by the use of more + n ?

Is it possible to get the first and stop right away searching? Thank for your help

Upvotes: 0

Views: 38

Answers (1)

Aacini
Aacini

Reputation: 67216

for /f "tokens=1* delims=:" %%a in (myfile.txt) do set line=%%a& goto breakFor
:breakFor
echo.%line%

You may also insert a line counter in the for cycle and break the for when a number of lines have been processed, but this method requires Delayed Expansion.

Upvotes: 1

Related Questions