Function - GetUSBHub()

Discuss and share scripts and script files...
Post Reply
highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Function - GetUSBHub()

Post by highend »

I was asked for help on a script that checks if more than one drive is connected to the same usb hub

This function takes a drive letter (either: X, X: or X:\) and if this drive is connected to an external
usb hub the hub's "USB Port Name old" is returned. By comparing this value for two calls (with two
drive letters) you can determine if they are on the same hub or not...

You need the command line utility "ListUsbDrives" from http://www.uwe-sieber.de/usbdlm.html#download
and edit the path to it in the function to get it working...

Code: Select all

// Get the "USB Port Name old" value from a log file created
// by ListUsbDrives (http://www.uwe-sieber.de/usbdlm.html#download)
// The function returns an empty string if the drive isn't connected to a hub
function GetUSBHub($drive, $logFile="") {
    // You need to adapt the path for $listUsbDrives for your system!
    if (!$logFile || exists($logFile) != 1) {
        $listUsbDrives = "D:\Users\Highend\Tools\USBDLM_x64\@ListUsbDrives\ListUsbDrives_x86.exe";
        $logFileContent = runret("$listUsbDrives", "%windir%"); }
    // $logFile given, it is a real file
    elseif (exists($logFile) == 1) { $logFileContent = readfile($logFile); }

    // Be a bit more tolerate on $drive definition
    $drive = replace($drive, "\");
    if !(regexmatches($drive, ":")) { $drive = $drive . ":"; }

    // Get drive's serial number
    $srcSerial = regexmatches(runret("cmd /c vol $drive", "%windir%"), "[a-f0-9-]{8,}");

    // Get the hub
    $usbHub = "";
    $serialFound = False;
    foreach($line, $logFileContent, <crlf>, "e") {
        if ($serialFound == True) {
            if (regexmatches($line, "^USB Port Name old")) {
                $usbHub = gettoken(gettoken($line, 2, "=", "t"), -2, "-", , 1);
                break;
            }
        }
        if (regexmatches($line, "^Volume Serial")) {
            if ($srcSerial == gettoken($line, 2, "=", "t")) { $serialFound = True; }
        }
    }
    return $usbHub;
}
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4140
Joined: 28 Feb 2012 19:27

Re: Function - GetUSBHub()

Post by klownboy »

Thanks highend, that may come in handy someday especially since I use many of Uwe Sieber's utilities anyway. By the way, what's your latest revision and timeline for UMC? I'm currently using v 0.1.4.2. If you need any further testing let me know. Better get it out before Don tackles CEAs. :)
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440

Post Reply