Page 1 of 1

Find will NOT find EXACT input!

Posted: 09 Jun 2020 13:25
by PAB
Good evening,

I have the following which I have spent hours on . . .

Code: Select all

@echo off

set KB_Number=
set /p KB_Number=^>Enter the KB Number: KB
if [%KB_Number%]==[] (
  echo  Invalid.
  pause
  goto :Exit
) else ( 
  echo  Checking if KB%KB_Number% is installed . . .
)
wmic qfe get hotfixid | find /I "KB%KB_Number%"
if %errorlevel% equ 1 (
  echo  NOT INSTALLED.
) else (
  echo  INSTALLED.
)
pause
goto :Exit

exit
It does work if I enter an installed KB number like KB2479943 for example.
But if I input KB247994 it is still searching for KB2479943.
I have tried . . .

Code: Select all

set KB_Number=""
set KB_Number=nul
. . . both at the top AND the bottom of the code without any luck!
I even tried findstr.

How do I get it to search for an EXACT match ONLY please?

Thanks in advance.

Re: Find will NOT find EXACT input!

Posted: 09 Jun 2020 14:03
by ShadowThief
wmic has pattern matching capabilities that you can take advantage of.

Code: Select all

@echo off

set KB_Number=
set /p "KB_Number=>Enter the KB Number: KB"
if "%KB_Number%"=="" (
  echo  Invalid.
  pause
  goto :Exit
) else ( 
  echo  Checking . . .
)
set "hotfixid="
for /f "tokens=1,2 delims==" %%A in ('wmic qfe where "hotfixid='KB%KB_Number%'" get hotfixid /value 2^>nul') do if not "%%B"=="" set "%%A=%%B"
if not defined hotfixid (
  echo  NOT INSTALLED.
) else (
  echo  INSTALLED.
)
pause

Re: Find will NOT find EXACT input!

Posted: 09 Jun 2020 14:17
by PAB
Thank you for the reply ShadowThief, it is very much appreciated!

I just came up with this . . .

Code: Select all

@echo off
set KB_Number=
set /p KB_Number=^>Enter the KB Number: 
if [%KB_Number%]==[] (
  echo  Invalid.
  pause
  goto :Exit
) else ( 
  echo  Checking if %KB_Number% is installed . . .
)
wmic qfe get hotfixid | findstr "\<%KB_Number%\>" >nul
if %errorlevel% equ 1 (
  echo  NOT INSTALLED.
) else (
  echo  INSTALLED.
)
pause
goto :Exit 
exit
. . .which seems to work!

I will have a look at your code tomorrow as I have to go to bed now because I have an early hospital appointment in the morning. Part of getting old I suppose.

Thanks again.

Re: Find will NOT find EXACT input!

Posted: 09 Jun 2020 15:20
by Squashman
Just a couple of pointers for you that you have been shown in your previous questions but have not caught onto. I would highly recommend you follow these.

undefined variable

Code: Select all

set "var="
Assigning a value to a variable

Code: Select all

set "var=value"
Comparing values

Code: Select all

IF "%var%"=="foo" command
Checking if a variable has a value

Code: Select all

IF NOT DEFINED var command
IF "%var%"=="" command