code in else statement not executing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
imyashy
Posts: 9
Joined: 10 Dec 2023 17:05

code in else statement not executing

#1 Post by imyashy » 31 Dec 2023 13:02

Hi people,

I put this script together with the following logic:

IF driver.inf is found output 'printer driver found' ELSE IF device instance id is found output 'reader ID found' ELSE 'reader id not found'

The first portion of the script works fine however the script then outputs Echo Printer Driver not found..Looking for Reader... and skips straight to echo reader ID not found even though the reader is definately present. Not sure what I am doing wrong.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "driverinf=magu.inf"
set "readerId=USB\VID_076B&PID_5320\OKCM0021111090337566741524390121"

REM Check if the printer driver exists in the output of pnputil
pnputil /enum-devices /connected /drivers /class Printer | findstr /C:"%driverinf%" >nul

REM If the printer driver is found, output 'printer found'
if %errorlevel% equ 0 (

Echo Printer Driver found..
 
) else (

Echo Printer Driver not found..Looking for Reader...

REM Check if the readerID exists in the output of pnputil
pnputil /enum-devices /connected | findstr /C:"%readerId%" >nul

REM If the device instance ID is found, output reader found
if %errorlevel% equ 0 (
    echo Reader ID found...
    
) else (
    echo reader ID not found.
)
)

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: code in else statement not executing

#2 Post by Batcher » 31 Dec 2023 21:56

1.bat

Code: Select all

@echo off
set "driverinf=magu.inf"
set "readerId=USB\VID_076B&PID_5320\OKCM0021111090337566741524390121"
pnputil /enum-devices /connected /drivers /class Printer | findstr /C:"%driverinf%" >nul
if %errorlevel% equ 0 (
    echo Printer Driver found..
) else (
    echo Printer Driver not found..Looking for Reader...
    pnputil /enum-devices /connected | findstr /C:"%readerId%" >nul
    if not errorlevel 1 (
        echo Reader ID found...
    ) else (
        echo reader ID not found.
    )
)

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: code in else statement not executing

#3 Post by Batcher » 31 Dec 2023 21:58

2.bat

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "driverinf=magu.inf"
set "readerId=USB\VID_076B&PID_5320\OKCM0021111090337566741524390121"
pnputil /enum-devices /connected /drivers /class Printer | findstr /C:"%driverinf%" >nul
if %errorlevel% equ 0 (
    echo Printer Driver found..
) else (
    echo Printer Driver not found..Looking for Reader...
    pnputil /enum-devices /connected | findstr /C:"%readerId%" >nul
    if !errorlevel! equ 0 (
        echo Reader ID found...
    ) else (
        echo reader ID not found.
    )
)
pause

imyashy
Posts: 9
Joined: 10 Dec 2023 17:05

Re: code in else statement not executing

#4 Post by imyashy » 02 Jan 2024 04:58

Batcher - Many thanks for your help.

2. bat worked. Good to see I needed to change %errorlevel% to !errorlevel! towards the end.

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

Re: code in else statement not executing

#5 Post by Squashman » 08 Jan 2024 09:49

imyashy wrote:
02 Jan 2024 04:58
Good to see I needed to change %errorlevel% to !errorlevel! towards the end.
You wouldn't need to use delayed expansion for the errorlevel if you would just adopt the builtin errorlevel usage of the IF command.

Code: Select all

 IF errorlevel 1 (
     echo reader ID not found.
) else (
    echo reader id found
)

Post Reply