using DOS batch, how to check, if afile.txt has the content, "Hello World!?.
content check
Moderator: DosItHelp
Re: content check
Hello,
You could use:
It will return each line that has 'hello, world' in it.
I think this is what you meant in your question :/
-- Phillid
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
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
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.
I didn't have time to test this. It's late here. I hope it works for you.
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.
Re: content check
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
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
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).