[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Free disk space with VMI in megabyte

Toki Toki

4/26/2008 5:48:00 PM

Hi all!

I'm new to ruby, I've found this pretty neat code linked on a thread on
this forum, original post was at:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://./root/cimv2")
disk = wmi.ExecQuery("Select * from Win32_LogicalDisk")
disk.each {|drive| puts "#{drive.DeviceID} #{drive.FreeSpace}"}

Now, the code work very well, but I can't convert the free space in MB
or any other unit, how can I convert it?

Thanks.

Best regards.
--
Posted via http://www.ruby-....

2 Answers

Michael Brooks

4/27/2008 2:37:00 AM

0

Toki Toki wrote:
> Hi all!
>
> I'm new to ruby, I've found this pretty neat code linked on a thread on
> this forum, original post was at:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...
>
> require 'win32ole'
>
> wmi = WIN32OLE.connect("winmgmts://./root/cimv2")
> disk = wmi.ExecQuery("Select * from Win32_LogicalDisk")
> disk.each {|drive| puts "#{drive.DeviceID} #{drive.FreeSpace}"}
>
> Now, the code work very well, but I can't convert the free space in MB
> or any other unit, how can I convert it?
>
> Thanks.
>
> Best regards.

Hello Toki:

The drive.FreeSpace is being returned as a string (which may also be a
nil). Change #{drive.FreeSpace} to #{drive.FreeSpace.class} to see what
I mean. You'll need to convert the FreeSpace to a number before you can
use math operators / functions with it. Below is an example of how to
do that with the #to_f (aka "to float") method:

disk.each {|drive| puts "#{drive.DeviceID} #{(drive.FreeSpace.to_f /
1024) / 1024}"}

Hope that helps,

Michael

Toki Toki

4/27/2008 8:54:00 PM

0

Michael Brooks wrote:
>
> Hello Toki:
>
> The drive.FreeSpace is being returned as a string (which may also be a
> nil). Change #{drive.FreeSpace} to #{drive.FreeSpace.class} to see what
> I mean. You'll need to convert the FreeSpace to a number before you can
> use math operators / functions with it. Below is an example of how to
> do that with the #to_f (aka "to float") method:
>
> disk.each {|drive| puts "#{drive.DeviceID} #{(drive.FreeSpace.to_f /
> 1024) / 1024}"}
>
> Hope that helps,
>
> Michael

Thanks a lot for the help and the explanation, I didn't know that
"FreeSpace" by default is returned as a string.

Best regards.
--
Posted via http://www.ruby-....