+ Reply to Thread
Results 1 to 17 of 17
  1. #1
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895

    VBScript vs. Ruby - Ruby rules!

    Ok came across a problem today
    We have one service that runs when a user logs on that drops a flag file in a directory.
    We have another service that runs that looks for those flag files and then does some work, then deletes the flag file.

    Well sometimes the 2nd service dies.
    So I wanted to put together a quick monitor script that will keep an eye on the folder to check for thenumber of flag files in this directory

    Mind you there are about 7000 files in this directory.. and the flag files are building up so we were at about 4000 flag files when I ran it.

    VBScript ~10 MINUTES
    Ruby ~ 10 SECONDS


    Now.. thats not all!

    VBScript Code
    Code:
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("f:\pathto\files")
    Set objFilesColl = objFolder.Files
    
    iFlgCount = 0
    For Each objFile In objFilesColl
      If UCase(objFSO.GetExtensionName(objFile.Name)) = "FLG" Then
        iFlgCount = iFlgCount + 1
      End If
    Next
    
    WScript.Echo iFlgCount
    Now for the ruby script

    bah.. don't need no stinkin' code tag for it
    too short

    ------------
    files = Dir.open("f:\\pathto\\server").entries.grep(/\.flg$/)
    puts files.size
    -----------


    Thats it!

    Now note I could of wrote the vbscript a little less verbose, and the ruby a little bit more verbose.. BUT it gives me the same number!
    I couldn't find a way to filter the collection in vbscript so I had to loop through each file manually taking quite a bit of time
    But with Ruby I just use the grep method and WHAM done!

    www.ruby-lang.org

    (note:
    --------------------------
    puts Dir.open("f:\\pathto\\server").entries.grep(/\.flg$/).size
    ------------------------
    also works )
    ONE LINE!
    Last edited by vass0922; October 21st, 2003 at 09:10 PM.

  2. #2
    Member
    Join Date
    Sep 2002
    Posts
    364
    Are you saying it took VBScript 10 minutes to count the files? How come you can't use a database.

  3. #3
    Ultimate Member Praetorian's Avatar
    Join Date
    Oct 2001
    Location
    Arizona
    Posts
    2,538
    Awesome vass. This may or may not be a fair comparison, but I'm writing a script in Python (newbie python programmer) logs information for the user currently logged in on a *nix box, and then logs some more information on the user's way out, then puts it into a MySQL db. There are two files, each with 20 some odd lines, so 40-50 lines total. A friend of mine is writing the same program on Windows in VB (newbie VB programmer too), and his program is already over 100 lines, and he's almost got the db connection working. Mind you we're both newbie programmers in these languages, and we're programming on different operating systems. Someone who is an expert in these langs could probably accomplish this same task in fewer lines....

  4. #4
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    errr huh?
    I'd still have to loop through the collection putting the data into the recordset then do a query?

    or are you talking about putting the flags into a DB?
    I didn't create the flag service/script but I do need to monitor it because of some stuff I added to it.
    So doing the DB thign would just add more unnecessary code when this monitor will work quite fine
    Especially in ruby
    I just added code to email me and it was only a few more lines
    Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.

  5. #5
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    ya VB's DB connections are quite verbose unfortunalty.

    I'm still a bit leery of ruby/python because they don't use variable declaration.
    But ruby does have a method that echo's all of the variables in the script so I could put that at the beginning to make sure I'm not misspelling something.
    like misspelling and mispeling are two distinct variables
    In VB you can put Option Explicit at the topand it requires variable declaration so the compiler will catch problems like that.
    But its pretty cool
    Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.

  6. #6
    Where's the beef? Scott Tiger's Avatar
    Join Date
    Mar 2002
    Location
    Southwest, VA
    Posts
    3,586
    I'll have to say I'm jealous not because of your scripting language skills - I'm jealous because you can just drop whatever interrepter you feel like in the box because you felt like it.

    We have strick standards at the railroad and we're not allowed to deviate from them even if it did make the job a lot easier.
    Where's Lunch?

  7. #7
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    ah, well its not standard here by any means.. and I certainly couldn't just go and start installing it on user machines.

    However, yes they do give me some leniancy.
    Just talked to one of our supervisors and he wants me to re-write to monitor the exchange IMC folders

    We have 5 bridgehead servers and a LOT of mail flow so its critical they don't die on us!


    If somebody can prove me wrong on the vbscript I'd be more than willing to try out other options

  8. #8
    Ultimate Member maface's Avatar
    Join Date
    Oct 2001
    Location
    MA
    Posts
    1,271
    Is Ruby similar to Python?

  9. #9
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    Not entirely sure quite honestly.

    Ruby was developed to be 100% object oriented
    So you can literally put "Some sort of string".to_i and it will convert the string to an integer.

    I only briefly looked at python before checking out ruby.
    There was never anything in python that made me go Ohhhh thats COOL!
    Now with ruby I say it all the time

    This may help you a bit
    http://www.ruby-doc.org/RubyEyeForThePythonGuy.html
    Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.

  10. #10
    Ultimate Member crouse's Avatar
    Join Date
    Jun 2002
    Location
    Iowa
    Posts
    2,879
    That URL was titled FUNNY VASS LOL


    Open Source RULES


    ------------------
    Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable.

    Oh, I need to mention, it's totally free, which means not only free of charge, but also freedom to use, copy, modify, and distribute it.
    Features of Ruby

    * Ruby has simple syntax, partially inspired by Eiffel and Ada.
    * Ruby has exception handling features, like Java or Python, to make it easy to handle errors.
    * Ruby's operators are syntax sugar for the methods. You can redefine them easily.
    * Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object, not in the sense of Python or Perl, but in the sense of Smalltalk: no exceptions. Example: In Ruby, the number 1 is an instance of class Fixnum.
    * Ruby's OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to add methods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behave differently from other instances of the same class.
    * Ruby features single inheritance only, *on purpose*. But Ruby knows the concept of modules (called Categories in Objective-C). Modules are collections of methods. Every class can import a module and so gets all its methods for free. Some of us think that this is a much clearer way than multiple inheritance, which is complex, and not used very often compared with single inheritance (don't count C++ here, as it has often no other choice due to strong type checking!).
    * Ruby features true closures. Not just unnamed function, but with present variable bindings.
    * Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.
    * Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don't have to care about maintaining reference counts in extension libraries. This is better for your health. ;-)
    * Writing C extensions in Ruby is easier than in Perl or Python, due partly to the garbage collector, and partly to the fine extension API. SWIG interface is also available.
    * Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances of class Fixnum) and large integers (Bignum), but you need not worry over which one is used currently. If a value is small enough, an integer is a Fixnum, otherwise it is a Bignum. Conversion occurs automatically.
    * Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple 'var' = local variable, '@var' = instance variable, '$var' = global variable. So it is also not necessary to use a tiresome 'self.' prepended to every instance member.
    * Ruby can load extension libraries dynamically if an OS allows.
    * Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading, regardless of if the OS supports it or not, even on MS-DOS! ;-)
    * Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/NT, Mac, BeOS, OS/2, etc.

    The Creator of Ruby

    Yukihiro Matsumoto, a.k.a Matz

    ---------- http://www.ruby-lang.org/en/20020101.html

  11. #11
    Ultimate Member maface's Avatar
    Join Date
    Oct 2001
    Location
    MA
    Posts
    1,271
    ask and you shall receive Thanks

  12. #12
    may contain mild peril SpookyEddy's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    3,329
    /me covers his eyes

    .... please for the love of all things good make the hungarian notation go away

    Ruby is a nice language IMO, I use it for a large proportion of the scripts I hack together.

    Regards

    ed
    I dreamt that a large eagle circled the room three times and then got into bed with me and took all the blankets.

  13. #13
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    actually I do like hungarian notation *shrug*

    Guess I've just been doing it for long
    Helps me remember what type it will be holding
    even if they are typeless I know what type it will be holding.

    Hey Eddy, have you had any problems with UNC naming?
    In this script I had to use a mapped drive because I couldnt' get a UNC name to work right.. it wouldnt' find any files in it
    I thought maybe it was just glob that was messing with me which is why I switched to .entries.grep, but it turns out itwas the UNC naming that messed me up
    Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.

  14. #14
    may contain mild peril SpookyEddy's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    3,329
    Heh, each to their own

    UNC works fine with Ruby (unless you have a really old version), but you need to reverse the slashes like so....
    Code:
    file = File.open("//scimitar/manage/test.txt", "r")
    file.each { |line| puts line }
    That works for me anyhoo.

    Later

    ed

  15. #15
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    hmm ok, I was still using backslashes .. but could of swore I did a path.tr("\\","/") to swap out the slashes... I'll try again when I get into work


    Thanks!

  16. #16
    may contain mild peril SpookyEddy's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    3,329
    np

    Just to check I tested it with Dir as well using...
    Code:
    dir = Dir.open("//scimitar/manage/")
    contents = dir.entries
    contents.each { |x| puts x }
    That also seems to work ok.

    Regards

    ed
    I dreamt that a large eagle circled the room three times and then got into bed with me and took all the blankets.

  17. #17
    Not Really a Member
    Join Date
    Oct 2001
    Posts
    27,895
    ahhhh got it

    This will get the path and threshhold of a list of folders from a comma delimited file. Then check if its past the threshhold for that particular folder will spit out an email.

    Code:
    def CheckFolder(psFolder, piThreshHold)
    
      puts "**** #{psFolder}"
      count = Dir.open(psFolder).entries.size
      puts psFolder + " count: " + count.to_s
      if count > piThreshHold.to_i then
        require 'net/smtp'
        server = 'smtpserver'
        smtp = Net::SMTP::new(server)
        smtp.start(server)
        smtp.ready('SendFormthisaddress@someplace.com', 'SendTOthisaddress@someplace.com') do |a|
          a.write "Subject: service stalled\r\n"
          a.write "There are currently " + count.to_s + " messages in folder\r\n"
          a.write "Path: #{psFolder}\r\n"
          a.write "This email is auto generated, please do not reply\r\n"
        end
      end
    
    end
    
    inputfile = File.new("MonitorFolders.csv")
    inputfile.readlines.each { |line|
      aData = line.split(",")
      sCurServer = aData[0].tr("\\","/")
      sThreshHold = aData[1]
      
      puts "Server: " + sCurServer + " ThreshHold: " + sThreshHold
      CheckFolder(sCurServer,sThreshHold)
    }
    Not bad
    short and sweet!
    Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.

Quick Reply Quick Reply

If you are already a member, please login above.

What is the color of the sky?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright 2014 All Enthusiast, Inc