Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Quick way to find all drives in a windows box (http://www.velocityreviews.com/forums/t809039-quick-way-to-find-all-drives-in-a-windows-box.html)

Damphyr 07-25-2003 10:39 PM

Quick way to find all drives in a windows box
 
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.


Bill Kelly 07-25-2003 11:15 PM

Re: Quick way to find all drives in a windows box
 
Hi,

From: "Damphyr" <damphyr@freemail.gr>
>
> 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




Damphyr 07-25-2003 11:21 PM

Re: Quick way to find all drives in a windows box
 
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.


Chris Morris 07-26-2003 12:27 AM

Re: Quick way to find all drives in a windows box
 
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






Damphyr 07-26-2003 01:41 AM

Re: Quick way to find all drives in a windows box
 
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.



All times are GMT. The time now is 10:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.