Find will NOT find EXACT input!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Find will NOT find EXACT input!

#1 Post by PAB » 09 Jun 2020 13:25

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.
Last edited by PAB on 09 Jun 2020 14:09, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Find will NOT find EXACT input!

#2 Post by ShadowThief » 09 Jun 2020 14:03

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Find will NOT find EXACT input!

#3 Post by PAB » 09 Jun 2020 14:17

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find will NOT find EXACT input!

#4 Post by Squashman » 09 Jun 2020 15:20

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

Post Reply