Page 1 of 1

code in else statement not executing

Posted: 31 Dec 2023 13:02
by imyashy
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.
)
)

Re: code in else statement not executing

Posted: 31 Dec 2023 21:56
by Batcher
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.
    )
)

Re: code in else statement not executing

Posted: 31 Dec 2023 21:58
by Batcher
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

Re: code in else statement not executing

Posted: 02 Jan 2024 04:58
by imyashy
Batcher - Many thanks for your help.

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

Re: code in else statement not executing

Posted: 08 Jan 2024 09:49
by Squashman
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
)