I want to check the status of the USB scan device connected to the remote computer using the Windows command line, cmd.exe
. (I only use Canon and Fijutsu brand scanners)
The command I use: pnputil /enum-devices > control.txt
I search for Canon Fijutsu
in TXT and find it, but the device status remains in the online/offline sub-lines. How can I filter them?
Sample TXT file:
Instance ID: ACPI\PNP0100\4&12567db6&0
Device Description: System timer
Class Name: System
Class GUID: {4d36e97d-e325-11ce-bfc1-08002be10318}
Manufacturer Name: (Standard system devices)
Status: Stopped
Driver Name: machine.inf
Instance ID: USB\VID_1083&PID_1661\5&886c6fc&0&2
Device Description: CANON DR-C240 USB
Class Name: Image
Class GUID: {6bdd1fc6-810f-11d0-bec7-08002be2092f}
Manufacturer Name: CANON
Status: Started
Driver Name: oem3.inf
Instance ID: ACPI\HPQ8002\4&12567db6&0
Device Description: Standard PS/2 Keyboard
Class Name: Keyboard
Class GUID: {4d36e96b-e325-11ce-bfc1-08002be10318}
Manufacturer Name: (Standard keyboards)
Status: Problem
Problem Code: 24 (0x18) [CM_PROB_DEVICE_NOT_THERE]
Driver Name: keyboard.inf
How do I get only these in this file;
CANON DR-C240 USB
Status: Started
When I try this command:
pnputil /enum-devices | findstr USB | Find /i "CANON Fijutsu"
...it gives the following output;
Device Description: CANON DR-C240 USB
I also want to see the status
section, but when I add status
in FIND
, it also brings up the status of other equipment.
You can't just findstr
over the complete command (as you noticed, it will find unrelated lines). Search for the Instance IDs first, then get the information for each of them separately, so you can easily filter for your desired information:
@echo off
for /f "tokens=2 delims=:" %%a in ('pnputil /enum-devices /class Mouse ^|find "Instance ID:"') do for /f "tokens=*" %%b in ("%%a") do call :getInfo "%%b"
goto :eof
:getinfo
for /f "tokens=2 delims=:" %%c in ('pnputil /enum-devices /InstanceID "%~1" ^| findstr /b "Device Status"') do for /f "tokens=*" %%d in ("%%c") do echo %%d
echo/
The second for
loops (%%b
, %%d
) are needed to remove the trailing spaces of the second token after separating by colon.
NOTE: the search strings are language dependent (the tokens are not due to the second for
loops instead of counting the tokens after separating them by spaces, which might seem to be the obvious solution, but for example Device Description
(two words in English) would be Gerätebeschreibung
(one word in German))
(judging by your example, your class is Image
- I used Mouse
to get a replicable output on systems without Scan-devices)
Same for direct use on command line (just in case you really need it there instead of using a readable reusable script):
@for /f "tokens=2 delims=:" %a in ('pnputil /enum-devices /class mouse ^|find "Instance ID:"') do @(for /f "tokens=*" %b in ("%a") do @for /f "tokens=2 delims=:" %c in ('pnputil /enum-devices /InstanceID "%~b" ^| findstr /b "Device Status"') do @for /f "tokens=*" %d in ("%c") do @echo %d)&@echo\