Quick way to find all drives in a windows box

D

Damphyr

Does anyone have a quick and elegant :) way to find all available drive
letters on a windows box?
I'm thinking in terms of Java File.listRoots() functionality.
Cheers,
V.-


____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
B

Bill Kelly

Hi,

From: "Damphyr said:
Does anyone have a quick and elegant :) way to find all available drive
letters on a windows box?
I'm thinking in terms of Java File.listRoots() functionality.

Here's one way:

# define the call:
require 'Win32API' => true
GetLogicalDriveStrings = Win32API.new("kernel32", "GetLogicalDriveStrings", ['L', 'P'], 'L')
=> #<Win32API:0x2ab0968>

# create a buffer to hold the result and execute the call:=> 40
# if successful, <len> is now the length of the null-terminated
# drive names appended into the buffer.... (if failed, <len> will
# be zero, or some size exceeding the length we passed in)

# looking at the raw data...
=> "A:\\\000C:\\\000D:\\\000E:\\\000F:\\\000G:\\\000M:\\\000P:\\\000T:\\\000Z:\\\000\000"

# split drive strings into array:
buf[0..len].split("\0")
=> ["A:\\", "C:\\", "D:\\", "E:\\", "F:\\", "G:\\", "M:\\", "P:\\", "T:\\", "Z:\\"]


Hope this helps,

Bill
 
D

Damphyr

Bill said:
Here's one way:

# define the call:

=> true
GetLogicalDriveStrings = Win32API.new("kernel32", "GetLogicalDriveStrings", ['L', 'P'], 'L')

=> #<Win32API:0x2ab0968>

# create a buffer to hold the result and execute the call:

=> 40
# if successful, <len> is now the length of the null-terminated
# drive names appended into the buffer.... (if failed, <len> will
# be zero, or some size exceeding the length we passed in)

# looking at the raw data...

=> "A:\\\000C:\\\000D:\\\000E:\\\000F:\\\000G:\\\000M:\\\000P:\\\000T:\\\000Z:\\\000\000"

# split drive strings into array:
buf[0..len].split("\0")

=> ["A:\\", "C:\\", "D:\\", "E:\\", "F:\\", "G:\\", "M:\\", "P:\\", "T:\\", "Z:\\"]


Hope this helps,

Well, yes and no :) I was hoping to avoid Win32API but I'll settle for
that now. Thanks,
V.-


____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
C

Chris Morris

Damphyr said:
Does anyone have a quick and elegant :) way to find all available
drive letters on a windows box?
I'm thinking in terms of Java File.listRoots() functionality.

Here's a routine I've got in my spare cl/util/win.rb file you can get
here http://www.clabs.org/dl/clutil/

# BSD Licensed code
module Windows
def Windows.drives(typeFilter=nil)
Drives::drives(typeFilter)
end

module Drives
GetDriveType = Win32API.new("kernel32", "GetDriveTypeA", ['P'], 'L')
GetLogicalDriveStrings = Win32API.new("kernel32", "GetLogicalDriveStrings", ['L', 'P'], 'L')

DRIVE_UNKNOWN = 0 # The drive type cannot be determined.
DRIVE_NO_ROOT_DIR = 1 # The root path is invalid. For example, no volume is mounted at the path.
DRIVE_REMOVABLE = 2 # The disk can be removed from the drive.
DRIVE_FIXED = 3 # The disk cannot be removed from the drive.
DRIVE_REMOTE = 4 # The drive is a remote (network) drive.
DRIVE_CDROM = 5 # The drive is a CD-ROM drive.
DRIVE_RAMDISK = 6 # The drive is a RAM disk.
DriveTypes = {
DRIVE_UNKNOWN => 'Unknown',
DRIVE_NO_ROOT_DIR => 'Invalid',
DRIVE_REMOVABLE => 'Removable/Floppy',
DRIVE_FIXED => 'Fixed',
DRIVE_REMOTE => 'Network',
DRIVE_CDROM => 'CD',
DRIVE_RAMDISK => 'RAM'
}

Drive = Struct.new('Drive', :name, :type, :typedesc)

def Drives.drives(typeFilter=nil)
driveNames = ' ' * 255
GetLogicalDriveStrings.Call(255, driveNames)
driveNames.strip!
driveNames = driveNames.split("\000")
drivesAry = []
driveNames.each do |drv|
type = GetDriveType.Call(drv)
if (!typeFilter) || (type == typeFilter)
drive = Drive.new(drv, type, DriveTypes[type])
drivesAry << drive
end
end
drivesAry
end
end
end
 
D

Damphyr

Tim said:
Damphyr graced us by uttering:



I'm afraid, since drive letters are a misfeature specific to the
Win32 platform, that by definition you're going to have problems
finding a platform-independent interface to it, elegant or
otherwise.

This task is accomplished with Perl using Win32::DriveInfo, which
in turn requires Win32::API. Likewise, this is served to Python
users via the win32api module.

Maybe if some more OSes appear that have the concept of drive
letters, and they gain a significant marketshare, we can offload
the Ruby interface to a 'Drives' module that internally
dispatches to Win32API, XsysAPI, YsysAPI, etc.; but I don't
expect it to be added to the Kernel namespace anytime soon.

Cheers,
Tim Hammerquist

I wouldn't dream of even daring to utter that the possibility had
crossed my mind. It took me about half an hour to write my own module to
get the drives. I was trying to avoid wading through the MSDN
documentation to find the right calls - being lazy as always.
Anyway, this kind of cruft gets on my nerves, trying to accomplish
anything in Windows is always a drag :(.
V.-


____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top