[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

WMI usage in Ruby to get file size, beginners question

Gaius Bonus

11/30/2006 2:05:00 PM

Hello,
sorry if the question is so stupid as I assume.
I try to get the size of a file by using wmi (because I want to collect
the size of various files on Microsoft boxes all over the network
later).

I try:

require 'win32ole'
mgmt = WIN32OLE.connect("winmgmts:\\\\.")
fiNa=mgmt.ExecQuery("SELECT * FROM CIM_DataFile WHERE Name =
'c:\\test.txt'")
fiNa.each {|fn| puts fn.FileSize.to_s}

I do not get any error message but also no output. So I think I do
something wrong with the wmi handling. I messed arround the whole day
and now I run out of ideas. It would be great if you could help me.
Thanks in advance,
bye

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

2 Answers

Ken Allen

11/30/2006 5:34:00 PM

0

Gaius Bonus wrote:
> Hello,
> sorry if the question is so stupid as I assume.
> I try to get the size of a file by using wmi (because I want to collect
> the size of various files on Microsoft boxes all over the network
> later).
>
> I try:
>
> require 'win32ole'
> mgmt = WIN32OLE.connect("winmgmts:\\\\.")
> fiNa=mgmt.ExecQuery("SELECT * FROM CIM_DataFile WHERE Name =
> 'c:\\test.txt'")
> fiNa.each {|fn| puts fn.FileSize.to_s}
>
> I do not get any error message but also no output. So I think I do
> something wrong with the wmi handling. I messed arround the whole day
> and now I run out of ideas. It would be great if you could help me.
> Thanks in advance,
> bye
>
>
\ is a control character in both in WQL and ruby so you need to escape
the slashes in the query twice:

fiNa=mgmt.ExecQuery("SELECT * FROM CIM_DataFile WHERE Name =
'c:\\\\test.txt'")

Also, the reason you weren't getting an error message about this seems
to be related to WMI often being asynchronous. If you had tried
fiNa.Count
it would have thrown an 80041017 error - a syntax error.

BTW I know nothing of WMI, I was just bored, curious, and feeling
generous so I basically did the googling for you :)

Ken

Gaius Bonus

11/30/2006 5:59:00 PM

0

Ken Allen wrote:
> Gaius Bonus wrote:
>> fiNa=mgmt.ExecQuery("SELECT * FROM CIM_DataFile WHERE Name =
>> 'c:\\test.txt'")
>> fiNa.each {|fn| puts fn.FileSize.to_s}
>>
>> I do not get any error message but also no output. So I think I do
>> something wrong with the wmi handling. I messed arround the whole day
>> and now I run out of ideas. It would be great if you could help me.
>> Thanks in advance,
>> bye
>>
>>
> \ is a control character in both in WQL and ruby so you need to escape
> the slashes in the query twice:
>
> fiNa=mgmt.ExecQuery("SELECT * FROM CIM_DataFile WHERE Name =
> 'c:\\\\test.txt'")
>
> Also, the reason you weren't getting an error message about this seems
> to be related to WMI often being asynchronous. If you had tried
> fiNa.Count
> it would have thrown an 80041017 error - a syntax error.
>
> BTW I know nothing of WMI, I was just bored, curious, and feeling
> generous so I basically did the googling for you :)
>
> Ken

Hello Ken,
thanks for the help, it was the hint. As I wrote I'm a beginner and I'm
not yet used to the specifics of everything. Ofcourse I also googled
before I asked but I did not find the \\\\.
thanks again
bye

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