WMI usage in Ruby to get file size, beginners question

G

Gaius Bonus

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
 
K

Ken Allen

Gaius said:
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
 
G

Gaius Bonus

Ken said:
\ 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top