# WMI usage in Ruby to get file size, beginners question

**URL:** <https://www.ruby-forum.com/t/wmi-usage-in-ruby-to-get-file-size-beginners-question/77783>\
**Category:** Ruby\
**Created:** [November 30, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/wmi-usage-in-ruby-to-get-file-size-beginners-question/77783 "2006-11-30T15:04:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zweibein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zweibein](https://www.ruby-forum.com/u/zweibein)\
**Post date:** [November 30, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/wmi-usage-in-ruby-to-get-file-size-beginners-question/77783/1 "2006-11-30T15:04:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zweibein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zweibein](https://www.ruby-forum.com/u/zweibein)\
**Post date:** [November 30, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/wmi-usage-in-ruby-to-get-file-size-beginners-question/77783/2 "2006-11-30T18:34:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zweibein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zweibein](https://www.ruby-forum.com/u/zweibein)\
**Post date:** [November 30, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/wmi-usage-in-ruby-to-get-file-size-beginners-question/77783/3 "2006-11-30T18:59:07Z")

</div>

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
