Quick way to find all drives in a windows box

Does anyone have a quick and elegant :slight_smile: 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.

Hi,

Does anyone have a quick and elegant :slight_smile: 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:

buf = “\0” * 1024
len = GetLogicalDriveStrings.call(buf.length, buf)
=> 40

if successful, is now the length of the null-terminated

drive names appended into the buffer… (if failed, will

be zero, or some size exceeding the length we passed in)

looking at the raw data…

buf[0…len]
=> “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

···

From: “Damphyr” damphyr@freemail.gr

Damphyr wrote:

Does anyone have a quick and elegant :slight_smile: 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

···


Chris
http://clabs.org/blogki

Bill Kelly wrote:

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:

buf = “\0” * 1024
len = GetLogicalDriveStrings.call(buf.length, buf)

=> 40

if successful, is now the length of the null-terminated

drive names appended into the buffer… (if failed, will

be zero, or some size exceeding the length we passed in)

looking at the raw data…

buf[0…len]

=> “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 :slight_smile: 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.

Damphyr graced us by uttering:

Hope this helps,

Well, yes and no :slight_smile: I was hoping to avoid Win32API but I’ll
settle for that now. Thanks,

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

···


To accuse others for one’s own misfortunes is a sign of want of education.
To accuse onself shows that one’s education has begun.
To accuse neither oneself nor others shows that one’s education is complete.
– Epictetus

Tim Hammerquist wrote:

Damphyr graced us by uttering:

Hope this helps,

Well, yes and no :slight_smile: I was hoping to avoid Win32API but I’ll
settle for that now. Thanks,

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.