WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: fhammer on December 22, 2016, 11:55:34 AM

Title: Find USB-attached device(s)
Post by: fhammer on December 22, 2016, 11:55:34 AM
Does anyone know a way to (within a WIL script) get a list of USB devices attached to the local system, or to search for a particular type of device?

Notes:

1) I was able to locate an attached USB modem by using serial extender function pComOpen, and looping through possible COM port ID's (COM1, COM2, ...) (with ERRORMODE temporarily OFF), but that involves a relatively-time-consuming exchange with the possible modem for each iteration).

2) I use a freeware utility called USBDEview (nirsoft) on my Windows 10 system to display the devices and types attached to my USB ports.
Title: Re: Find USB-attached device(s)
Post by: Russell_Williams on December 22, 2016, 01:34:42 PM
I have used the DiskScan function to get drives.

hd = DiskScan(2)
Message("Hard drives on system", hd)


Title: Re: Find USB-attached device(s)
Post by: td on December 22, 2016, 02:03:01 PM
DirScan(64) will give you a list of USB drives.  For a more general reckoning WMI is an options:

Code (winbatch) Select
strComputer = '.'

objWMIService = GetObject('winmgmts:\\':strComputer:'\root\cimv2')
colDevices = objWMIService.ExecQuery('Select * From Win32_USBControllerDevice')
ForEach objDevice in colDevices
   strDeviceName = objDevice.Dependent
   strDeviceName = StrReplace(strDeviceName, '"', '')
   strDeviceName = ItemExtract(2, strDeviceName, '=')

   colUSBDevices = objWMIService.ExecQuery(:"Select * From Win32_PnPEntity Where DeviceID = '":strDeviceName:"'")
   ForEach objUSBDevice in colUSBDevices
      if ObjectTypeGet(objUSBDevice) != 'EMPTY'
         Pause('Device Descript', objUSBDevice.Description)
      endif
   Next   
Next


Search on the WMI class names on MSFT's Website to find additional properties that might be of interest.
Title: Re: Find USB-attached device(s)
Post by: fhammer on December 22, 2016, 03:04:58 PM
Thanks all. The DiskScan options only list drives. I will try the WMI option to get info on other types of USB-connected devices. WMI is new to me, so it may take a while. However, I'll reply back.