Quick way to find all drives in a windows box

Discussion in 'Ruby' started by Damphyr, Jul 25, 2003.

  1. Damphyr

    Damphyr Guest

    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.
     
    Damphyr, Jul 25, 2003
    #1
    1. Advertising

  2. Damphyr

    Bill Kelly Guest

    Hi,

    From: "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.


    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, <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...
    >> 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
     
    Bill Kelly, Jul 26, 2003
    #2
    1. Advertising

  3. Damphyr

    Damphyr Guest

    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, <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...
    >
    >>>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 :) 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, Jul 26, 2003
    #3
  4. Damphyr

    Chris Morris Guest

    Damphyr wrote:

    > 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

    --
    Chris
    http://clabs.org/blogki
     
    Chris Morris, Jul 26, 2003
    #4
  5. Damphyr

    Damphyr Guest

    Tim Hammerquist wrote:
    > Damphyr graced us by uttering:
    >
    >>>Hope this helps,

    >>
    >>Well, yes and no :) 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.
     
    Damphyr, Jul 26, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=

    Enumerate all the drives including Mapped network drives on a serv

    =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=, Feb 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,058
    =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=
    Feb 26, 2005
  2. kalbee
    Replies:
    2
    Views:
    387
    steph
    Oct 12, 2004
  3. Replies:
    9
    Views:
    492
    Phlip
    Jun 10, 2006
  4. bill

    Quick way to find all tds?

    bill, Feb 12, 2004, in forum: Javascript
    Replies:
    3
    Views:
    117
    Michael Winter
    Feb 12, 2004
  5. lofenee
    Replies:
    3
    Views:
    141
    Ben Bullock
    Apr 22, 2008
Loading...

Share This Page