Skip to main content
deleted 532 characters in body; added 158 characters in body
Source Link
jason
  • 242.6k
  • 35
  • 436
  • 532

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file | find /c ": 0"
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo not foundnotfound
goto done
:done

This is really a proof of concept; clean up as it suits your needs.

Explanation:

find /c "string" file

prints out a count of the number of times string appeared in file. It's output looks like this:

---------- FILE: #

where #The key is the number of times string appeared inthat filefind. Then I pipe this to returns an finderrorlevel again looking forof : 01 to indicate thatif string appearedis not in file zero times. find will return an errorlevel of 1 to indicate success (i.e., that the count was zero). In this case weWe branch to notfound. Otherwise in this case otherwise we handle the found case.

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file | find /c ": 0"
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo not found
goto done
:done

This is really a proof of concept; clean up as it suits your needs.

Explanation:

find /c "string" file

prints out a count of the number of times string appeared in file. It's output looks like this:

---------- FILE: #

where # is the number of times string appeared in file. Then I pipe this to find again looking for : 0 to indicate that string appeared in file zero times. find will return an errorlevel of 1 to indicate success (i.e., that the count was zero). In this case we branch to notfound. Otherwise we handle the found case.

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done

This is really a proof of concept; clean up as it suits your needs. The key is that find returns an errorlevel of 1 if string is not in file. We branch to notfound in this case otherwise we handle the found case.

Source Link
jason
  • 242.6k
  • 35
  • 436
  • 532

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file | find /c ": 0"
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo not found
goto done
:done

This is really a proof of concept; clean up as it suits your needs.

Explanation:

find /c "string" file

prints out a count of the number of times string appeared in file. It's output looks like this:

---------- FILE: #

where # is the number of times string appeared in file. Then I pipe this to find again looking for : 0 to indicate that string appeared in file zero times. find will return an errorlevel of 1 to indicate success (i.e., that the count was zero). In this case we branch to notfound. Otherwise we handle the found case.