slloyd has asked for the wisdom of the Perl Monks concerning the following question:

I have the following script that I use to determine drive types. Most USB drives return as a Removable Drive type, but the Lexar Jumpdrive returns as a Fixed Drive. Does anyone know of a better way to determine if a removable drive is plugged in to the computer (Camera, USB Drive, etc)? Is there a way to determine if a removable drive is a USB drive?
use strict; use Win32::API; $|=1; binmode(STDOUT); getDrivesInUse(); ############### sub getDrivesInUse{ my (@dr, $i); my %DriveType=( 1=>"Unknown Root", 2=>"Removable", 3=>"Fixed", 4=>"Mapped", 5=>"CDRom", 6=>"RAMDisk", ); my $GetLogicalDrives = new Win32::API("kernel32", "GetLogicalDrive +s", [], 'N') || return $^E; my $bitmask = $GetLogicalDrives->Call(); print "bitmask:\r\n$bitmask\r\n\r\n"; for $i(0..31) { if ($bitmask & 2**$i){ my $drive=chr(ord("A")+$i); my $type=getDriveType($drive); print "$i\. Drive: $drive [$type][$DriveType{$type}]\r\n"; } } } ############### sub getDriveType ($) { my $drive = shift; return undef unless $drive =~ s/^([a-z])(:(\\)?)?$/$1:\\/i || $dri +ve =~ s/^(\\\\\w+\\\w+\$?)(\\)?$/$1\\/; my $GetDriveType = new Win32::API("kernel32", "GetDriveType", ['P' +], 'N') or return; my ($lpDirectoryName) = $drive; my $type = $GetDriveType->Call( $lpDirectoryName ); return $type; }

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: Detecting a Lexar Jumpdrive
by jasonk (Parson) on Nov 29, 2005 at 05:48 UTC

    Is there a way to determine if a removable drive is a USB drive?

    What you seem to actually be asking is "is there a way to determine if a given drive is a removable USB drive?" Generally speaking, the answer is probably no. The concept of "removable" drive is a bit too abstract to really deal with effectively in software. Is an ATAPI drive in an external USB case a removable USB drive? What about SCSI drives in hot-swappable RAID trays? What about a firewire attached drive that is inside the computers case?


    We're not surrounded, we're in a target-rich environment!