Find command gives wrong errorlevel

I need to run a command to find a string in a txt file and if it exists echo Found to either another txt file or the screen.

The problem is I get inconsistent errorlevels. I have output the wmic qfe to a file named wmicinfo.txt.  I then run the command below and get one output the firsttime, then a different output thereafter.

run 1st time...
type wmicinfo.txt | find /c "KB3999999" | echo %errorlevel%
OUTPUT= 1

run 2nd time and 3rd and so on...
type wmicinfo.txt | find /c "KB3999999" | echo %errorlevel%
OUTPUT= 0
cavalierlanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
OK this is because the whole line is interpreted in one go so the %errorlevel% isn't set until the next line.  You can probably either do this with delayed expansion:

@echo off
setlocal enabledelayedexpansion
type wmicinfo.txt | find /c "KB3999999" | echo !errorlevel!

or maybe neater?

@echo off
find /c "KB3999999" < wmicinfo.txt && echo Found

The || will run the next command based on a fail (errorlevel 1+) or && will run it based on success (0)

Steve

[Edit, sorry put || instead of &&]
0
Steve KnightIT ConsultancyCommented:
You can also do more than one item in brackets based on the find using && and || e.g.

find /c "KB3999999" < wmcinfo.txt && (
  echo It has been found
  echo DO this
) || (
  echo Do somehing else
)

Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
And just to spice it up a bit, you can also get away from the whole error level checking.  Since you are just looking for the count you can check that value and act accordingly, like below.  Lots of ways to approach this...

for /f %%A in ('find /c "KB3999999" ^< wmcinfo.tx') do (
  if %%A == 0 (
    echo NOT FOUND
  ) else (
    echo FOUND
  )
)

Open in new window

~bp
0
cavalierlanAuthor Commented:
I used the second post.  It worked perfect.  I changed a little to get it to work in my for loop but otherwise turned out consistent results.

I was looking for error checking because I wanted to echo the KBnumber with either the Found or didn't Find results in my for loop.
0
Steve KnightIT ConsultancyCommented:
Thanks, glad it helped..... always a million different obscure ways to do the same thing in batch!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.