Page 1 of 1

"For" string needs to process only one line

Posted: 01 Jan 2012 02:23
by Rileyh
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.

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

Posted: 01 Jan 2012 07:39
by aGerman
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

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

Posted: 02 Jan 2012 03:58
by Rileyh
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

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

Posted: 02 Jan 2012 04:14
by orange_batch
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
))

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

Posted: 03 Jan 2012 21:08
by 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 ( ...


- 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.

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

Posted: 08 Jan 2012 05:11
by Rileyh
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