"For" string needs to process only one line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

"For" string needs to process only one line

#1 Post by Rileyh » 01 Jan 2012 02:23

I have a for string:

Code: Select all

REM Printline:
REM Standard printline:
set "$file=test.txt"
for /f "tokens=1*" %%a in ('findstr /b /i "printline" "!$file!"') do (
  set "command=%%a"
  set "value=%%b"
  echo !value!
  goto :break
)

:break


I need it to process only the first line of test.txt. It currently "picks out" printline from the first point in test.txt, not necessarily the first line.
Can one do that?

Regards,
Rileyh

Edited by aGerman: Code tags corrected.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: "For" string needs to process only one line

#2 Post by aGerman » 01 Jan 2012 07:39

Try:

Code: Select all

@echo off &setlocal DisableDelayedExpansion

set "$file=test.txt"

for /f "tokens=1*" %%a in ('findstr /b /i "printline" "%$file%"') do (
  set "command=%%a"
  set "value=%%b"

  setlocal EnableDelayedExpansion
  echo !command!
  echo !value!
  endlocal

  goto :break
)

:break
echo(
echo "%command%"
echo "%value%"
pause

Regards
aGerman

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: "For" string needs to process only one line

#3 Post by Rileyh » 02 Jan 2012 03:58

Unfortunately this didn't work as I wanted it to. It builds correctly, but it "picks out" the string, rather than breaking the "for /f" loop if it is unsuccessful on the first line.
By the way, how can I get it to read each line successively. E.g.

Code: Select all

:loop
find /c /i "(string)" "(file)" (in line 1)
(same thing)
(same thing)
goto :loop

And when it goes to :loop, it reads the second line. And on the third time through, it reads the fourth line. And so on, until it reaches the end, where it exits. And we don't know how long the file being searched in will be.

Regards,
Rileyh

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: "For" string needs to process only one line

#4 Post by orange_batch » 02 Jan 2012 04:14

I need it to process only the first line of test.txt.

Code: Select all

REM Printline:
REM Standard printline:
set "$file=test.txt"
for /f "usebackq tokens=1*" %%a in ("!$file!") do (
  set "check=%%a"
  if "!check:~,9!"=="printline" (
  set "command=%%a"
  set "value=%%b"
  echo !value!
)
  goto :break
)
:break

By the way, how can I get it to read each line successively

I don't understand the purpose of this, but... (better)

Code: Select all

REM Printline:
REM Standard printline:
set "$file=test.txt"
set linenum=-1
:loop
set /a linenum+=1
for /f "tokens=1*" %%a in ('more +%linenum% "!$file!"') do (
  set "check=%%a"
  if "!check:~,9!"=="printline" (
  set "command=%%a"
  set "value=%%b"
  echo !value!
)
  goto loop
)

or... (worse)

Code: Select all

REM Printline:
REM Standard printline:
set "$file=test.txt"
set linenum=
:loop
set /a linenum+=1
set linecount=
for /f "usebackq tokens=1*" %%a in ("!$file!") do (
  set /a linecount+=1
  if !linecount!==%linenum% (
  set "check=%%a"
  if "!check:~,9!"=="printline" (
  set "command=%%a"
  set "value=%%b"
  echo !value!
)
  goto loop
))

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: "For" string needs to process only one line

#5 Post by Aacini » 03 Jan 2012 21:08

Let's do some clarifications:

- If you want to process the first line of test.txt file, you may do it this way:

Code: Select all

set /P firstLine=< "%$file%"
For example:

Code: Select all

for /F "tokens=1*" %%a in ("%firstline%") do ( ...


- On the other hand, if you want to process the first line of the result of findstr, you may do it this way:

Code: Select all

findstr /b /i "printline" "%$file%" > findstrout.txt
set /P firstLine=< findstrout.txt


- Finally, you may read each line successively this way:

Code: Select all

call :ProcessEachLine < anyfile.txt
goto :eof

:ProcessEachLine
set line=:EOF
set /P line=
if "%line%" == ":EOF" exit /B
echo Process line here in any way you wish
goto ProcessEachLine

Previous loop ends at the first empty line, or file end. If the file may contain empty lines, an additional trick is required to insert line numbers with FINDSTR /N and remove they before process the line.

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: "For" string needs to process only one line

#6 Post by Rileyh » 08 Jan 2012 05:11

Aacini,
Let's do some clarifications:

- If you want to process the first line of test.txt file, you may do it this way:

Code: Select all

set /P firstLine=< "%$file%"

For example:

Code: Select all

for /F "tokens=1*" %%a in ("%firstline%") do ( ...



Is it possible to extend that to a number previously set?
E.g:

Code: Select all

set "line=(number)"
set /p %line%=(something)


Regards,
Rileyh

Post Reply