content check

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

content check

#1 Post by glob5 » 13 Sep 2011 00:08

using DOS batch, how to check, if afile.txt has the content, "Hello World!?.
:?

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: content check

#2 Post by phillid » 13 Sep 2011 00:38

Hello,
You could use:

Code: Select all

findstr /i "hello, world" afile.txt


It will return each line that has 'hello, world' in it.

I think this is what you meant in your question :/

-- Phillid

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: content check

#3 Post by glob5 » 13 Sep 2011 03:49

has to check a value, i.e., 0 if string not found in afile.txt, 1 if string found in afile.txt or vice versa. how?. using errorlevel?.
:?

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: content check

#4 Post by Bob D » 13 Sep 2011 05:08

Here is a useful command refernce http://www.microsoft.com/download/en/confirmation.aspx?id=2632
The Findstr does not appear to set errorlevel. You could try using the /m to only output the filename if the string exists within the file.
You could put that filename into another file and read back the content of that file. If none then not found.

Code: Select all

findstr /i /m "hello,world" afile.txt > bfile.txt
set /p found < bfile.txt
if defined found (echo 1) else (echo 0)

I didn't have time to test this. It's late here. I hope it works for you.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: content check

#5 Post by dbenham » 13 Sep 2011 11:41

FINDSTR does set the ERRORLEVEL :!:

ERRORLEVEL = 0 if string is found and 1 if string is not found.

Since in this case we only want the answer to "Does the string exist in the file", then the output should be redirected to NUL.

FINDSTR parses the search string into tokens, so FINDSTR "Hello World!" afile.txt>NUL will set ERRORLEVEL to 0 if it finds any line that contains "Hello" or "World!". If you want to search for the string literal "Hello World!" then you need to use the /C option. FINDSTR /C:"Hello World!" afile.txt>NUL

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: content check

#6 Post by Bob D » 13 Sep 2011 13:59

dbenham wrote:FINDSTR does set the ERRORLEVEL :!:

ERRORLEVEL = 0 if string is found and 1 if string is not found.


One of the things I find frustrating is there seems to be no fully accurate command reference. Is there any such thing :?: I looked for info on Findstr setting Errorlevel but could not find it. It seems obvious that it should so you can test a result (easily). :? :x

Post Reply