WMI usage in Ruby to get file size, beginners question

Discussion in 'Ruby' started by Gaius Bonus, Nov 30, 2006.

  1. Gaius Bonus

    Gaius Bonus Guest

    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-forum.com/.
     
    Gaius Bonus, Nov 30, 2006
    #1
    1. Advertising

  2. Gaius Bonus

    Ken Allen Guest

    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
     
    Ken Allen, Nov 30, 2006
    #2
    1. Advertising

  3. Gaius Bonus

    Gaius Bonus Guest

    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-forum.com/.
     
    Gaius Bonus, Nov 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santiago  Romero

    How to get memory size/usage of python object

    Santiago Romero, Jan 9, 2008, in forum: Python
    Replies:
    5
    Views:
    959
    Steven D'Aprano
    Jan 10, 2008
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,743
    Michael Jung
    May 25, 2008
  3. Phillip Gawlowski

    [Update] Ruby Zen: Ruby for Beginners

    Phillip Gawlowski, Jan 22, 2010, in forum: Ruby
    Replies:
    0
    Views:
    131
    Phillip Gawlowski
    Jan 22, 2010
  4. Sandeep Guria
    Replies:
    5
    Views:
    148
    Sandeep Guria
    Feb 25, 2011
  5. kngnsm
    Replies:
    0
    Views:
    221
    kngnsm
    Aug 24, 2004
Loading...

Share This Page