FOR /f "tokens=4,5" %i in (file.txt) DO if %j == sometext echo %i %j
outputs desired content, but I want processing to quit upon first occurrence,
and not by exiting the console.
FOR loop : make it stop itself
Re: FOR loop : make it stop itself
This will echo just the first result found.
It will not stop processing the entire file.
It will not stop processing the entire file.
Code: Select all
set "found=" & FOR /f "tokens=4,5" %i in (file.txt) DO @if not defined found if "%j" == "sometext" echo %i %j & set "found=1"
Re: FOR loop : make it stop itself
You can do it inside a script;
Code: Select all
@echo off
FOR /f "tokens=4,5" %%i in (file.txt) DO (
if "%%j" == "sometext" echo %%i %%j
if "%%j" == "sometext" goto Label
)
goto:eof
:Label
echo Match found. Processing stopped.
It can be made a little more flexible with;
Code: Select all
@echo off
FOR /f "tokens=4,5" %%i in (%1) DO (
if "%%j" == "%2" echo %%i %%j
if "%%j" == "%2" goto Label
)
goto:eof
:Label
echo Match found. Processing stopped.
file-search.bat file.txt sometext
I'm sure there's some reason why doing this is very, very bad, but it works in all my tests and as long as you aren't doing it dozens or hundreds of times, it shouldn't cause any major problems.