Page 1 of 1
content check
Posted: 13 Sep 2011 00:08
by glob5
using DOS batch, how to check, if afile.txt has the content, "Hello World!?.
Re: content check
Posted: 13 Sep 2011 00:38
by phillid
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
Re: content check
Posted: 13 Sep 2011 03:49
by glob5
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?.
Re: content check
Posted: 13 Sep 2011 05:08
by Bob D
Here is a useful command refernce
http://www.microsoft.com/download/en/confirmation.aspx?id=2632The 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.
Re: content check
Posted: 13 Sep 2011 11:41
by dbenham
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
Re: content check
Posted: 13 Sep 2011 13:59
by Bob D
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).