Findstr question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Findstr question

#1 Post by MrKnowItAllxx » 04 Aug 2012 21:30

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Findstr question

#2 Post by Ed Dyreen » 04 Aug 2012 21:36

'
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]

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Findstr question

#3 Post by MrKnowItAllxx » 04 Aug 2012 21:47

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Findstr question

#4 Post by Ed Dyreen » 04 Aug 2012 22:54

'

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

something

Code: Select all

found something in file.
found something in file
Druk op een toets om door te gaan. . .
'file.txt'

Code: Select all

nothing

Code: Select all

not found something in file.
not found something in file
Druk op een toets om door te gaan. . .

Post Reply