Page 1 of 1
Findstr question
Posted: 04 Aug 2012 21:30
by MrKnowItAllxx
Maybe I am missing something simple, but I would like to use findstr to search for a line in a file, and parse that line to give output (a for loop works). I have that down, but I would also like the script to alert the user if no match is found.
So far I have this:
for /f "delims=." %%f in ('findstr /i /c:"%%d" "file.txt"') do echo [%%f]
By "parse" the line I mean that I would like to take only the text before the first "." in the file, but again I need to know if the line wasn't found in the first place.
Re: Findstr question
Posted: 04 Aug 2012 21:36
by Ed Dyreen
'
only the text before the first "." if the line was found.
Code: Select all
for /f "tokens=1 delims=." %%f in ( '2^>nul findstr /lic:"%%~d" "file.txt"' ) do echo.[%%f]
Re: Findstr question
Posted: 04 Aug 2012 21:47
by MrKnowItAllxx
Your code works just fine, but what I need is for there to be a message printed on the screen if the search string (%%d) is not found in the file.
Re: Findstr question
Posted: 04 Aug 2012 22:54
by Ed Dyreen
'
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$file=file.txt"
set "$str=something"
2>&1 >nul findstr /lic:"!$str!" "!$file!" &&echo.found !$str! in file. ||echo.not found !$str! in file.
for /f "tokens=1 delims=." %%? in (
'2^>^&1 ^>nul findstr /lic:"!$str!" "!$file!" ^&^&echo.found !$str! in file. ^|^|echo.not found !$str! in file.'
) do echo.%%?
pause
exit
'file.txt'
Code: Select all
found something in file.
found something in file
Druk op een toets om door te gaan. . .
'file.txt'
Code: Select all
not found something in file.
not found something in file
Druk op een toets om door te gaan. . .