Error when running script

Discussion in 'Ruby' started by Brad Winborg, Mar 1, 2009.

  1. Brad Winborg

    Brad Winborg Guest

    This is the script, it's intent is to read all Error logs.txt files in a
    directory or just the text files that are listed. Find all the lines
    that contain the word "error" and all the lines that contain the word
    "warning" display a count of each word. Additionally it should create
    report that tells how many times the two words appear every ten min, for
    the duration of the error logs.

    my_path = "D:\My Documents\\Replicon\\log_file_20090221_041817.txt"

    warning = 0
    error = 0

    d = Dir.new(my_path)
    d.each do |fl|
    unless fl == '.' or fl == '..' then
    puts my_path + fl
    File.open(my_path + fl, "r").each do |line|
    if line =~ /error/ then
    error += 1
    elsif line =~ /warning/ then
    warning += 1
    end
    end

    end
    end
    puts warning.to_s
    puts error.to_s


    Here is the errors I receive when I run it.

    Not a directory - D:\My Documents\Replicon\log_file_20090221_041817.txt\
    D:/My Documents/Replicon/ErrorCount.rb:6:in `initialize'
    Press ENTER to close the window...

    Directory Path
    D:\My Documents\Replicon
    I was going to add an attachment to show that the directory really exits
    and that the file is in that directory, but it was to large.

    If anyone has any ideas as to why it is not working I be very grateful,
    as I am new to ruby code in general.
    --
    Posted via http://www.ruby-forum.com/.
    Brad Winborg, Mar 1, 2009
    #1
    1. Advertising

  2. Brad Winborg

    Tim Hunter Guest

    Brad Winborg wrote:
    > This is the script, it's intent is to read all Error logs.txt files in a
    > directory or just the text files that are listed. Find all the lines
    > that contain the word "error" and all the lines that contain the word
    > "warning" display a count of each word. Additionally it should create
    > report that tells how many times the two words appear every ten min, for
    > the duration of the error logs.
    >
    > my_path = "D:\My Documents\\Replicon\\log_file_20090221_041817.txt"
    >
    > warning = 0
    > error = 0
    >
    > d = Dir.new(my_path)
    > d.each do |fl|
    > unless fl == '.' or fl == '..' then
    > puts my_path + fl
    > File.open(my_path + fl, "r").each do |line|
    > if line =~ /error/ then
    > error += 1
    > elsif line =~ /warning/ then
    > warning += 1
    > end
    > end
    >
    > end
    > end
    > puts warning.to_s
    > puts error.to_s
    >
    >
    > Here is the errors I receive when I run it.
    >
    > Not a directory - D:\My Documents\Replicon\log_file_20090221_041817.txt\
    > D:/My Documents/Replicon/ErrorCount.rb:6:in `initialize'
    > Press ENTER to close the window...
    >
    > Directory Path
    > D:\My Documents\Replicon
    > I was going to add an attachment to show that the directory really exits
    > and that the file is in that directory, but it was to large.
    >
    > If anyone has any ideas as to why it is not working I be very grateful,
    > as I am new to ruby code in general.


    Look at the error message. You're asking Dir.new to open the directory
    "D:\My Documents\Replicon\log_file_20090221_041817.txt".
    I think the directory name is actually "D:\My Documents\Replicon" and
    "log_file_20090221_041817.txt" is a file within that directory.

    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Mar 1, 2009
    #2
    1. Advertising

  3. Brad Winborg

    Tim Greer Guest

    Brad Winborg wrote:

    > This is the script, it's intent is to read all Error logs.txt files in
    > a directory or just the text files that are listed. Find all the lines
    > that contain the word "error" and all the lines that contain the word
    > "warning" display a count of each word. Additionally it should create
    > report that tells how many times the two words appear every ten min,
    > for the duration of the error logs.
    >
    > my_path = "D:\My Documents\\Replicon\\log_file_20090221_041817.txt"
    >
    > warning = 0
    > error = 0
    >
    > d = Dir.new(my_path)
    > d.each do |fl|
    > unless fl == '.' or fl == '..' then
    > puts my_path + fl
    > File.open(my_path + fl, "r").each do |line|
    > if line =~ /error/ then
    > error += 1
    > elsif line =~ /warning/ then
    > warning += 1
    > end
    > end
    >
    > end
    > end
    > puts warning.to_s
    > puts error.to_s
    >
    >
    > Here is the errors I receive when I run it.
    >
    > Not a directory - D:\My
    > Documents\Replicon\log_file_20090221_041817.txt\ D:/My
    > Documents/Replicon/ErrorCount.rb:6:in `initialize' Press ENTER to
    > close the window...
    >


    Looks like you tried to open a file as a directory:

    D:\My Documents\Replicon\log_file_20090221_041817.txt

    The path looks to be:

    D:\My Documents\Replicon\

    And the file in that path:

    log_file_20090221_041817.txt


    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Mar 1, 2009
    #3
  4. Brad Winborg

    Brad Winborg Guest

    Tim Hunter wrote:
    > Brad Winborg wrote:
    >> error = 0
    >> end
    >> Not a directory - D:\My Documents\Replicon\log_file_20090221_041817.txt\
    >> D:/My Documents/Replicon/ErrorCount.rb:6:in `initialize'
    >> Press ENTER to close the window...
    >>
    >> Directory Path
    >> D:\My Documents\Replicon
    >> I was going to add an attachment to show that the directory really exits
    >> and that the file is in that directory, but it was to large.
    >>
    >> If anyone has any ideas as to why it is not working I be very grateful,
    >> as I am new to ruby code in general.

    >
    > Look at the error message. You're asking Dir.new to open the directory
    > "D:\My Documents\Replicon\log_file_20090221_041817.txt".
    > I think the directory name is actually "D:\My Documents\Replicon" and
    > "log_file_20090221_041817.txt" is a file within that directory.


    Thank you that did help, but I still have a problem.

    my_path = "C:\\Ruby\\Text\\"

    warning = 0
    error = 0

    d = Dir.new(my_path)
    d.each do |fl|
    unless fl == '.' or fl == '..' then
    puts my_path + fl
    File.open(my_path + fl, "r").each do |line|
    if line =~ /error/ then
    error += 1
    elsif line =~ /warning/ then
    warning += 1
    end
    end

    end
    end
    puts warning.to_s
    puts error.to_s

    The problem that I have now is that it apparently sees the file but does
    not read it.

    Below is the response that is returned.

    C:\Ruby\Text\log_file_20090221_041817.txt
    0
    0
    Press ENTER to close the window...

    --
    Posted via http://www.ruby-forum.com/.
    Brad Winborg, Mar 1, 2009
    #4
  5. Brad Winborg

    Tim Hunter Guest

    Brad Winborg wrote:
    > Tim Hunter wrote:
    >> Brad Winborg wrote:
    >>> error = 0
    >>> end
    >>> Not a directory - D:\My Documents\Replicon\log_file_20090221_041817.txt\
    >>> D:/My Documents/Replicon/ErrorCount.rb:6:in `initialize'
    >>> Press ENTER to close the window...
    >>>
    >>> Directory Path
    >>> D:\My Documents\Replicon
    >>> I was going to add an attachment to show that the directory really exits
    >>> and that the file is in that directory, but it was to large.
    >>>
    >>> If anyone has any ideas as to why it is not working I be very grateful,
    >>> as I am new to ruby code in general.

    >> Look at the error message. You're asking Dir.new to open the directory
    >> "D:\My Documents\Replicon\log_file_20090221_041817.txt".
    >> I think the directory name is actually "D:\My Documents\Replicon" and
    >> "log_file_20090221_041817.txt" is a file within that directory.

    >
    > Thank you that did help, but I still have a problem.
    >
    > my_path = "C:\\Ruby\\Text\\"
    >
    > warning = 0
    > error = 0
    >
    > d = Dir.new(my_path)
    > d.each do |fl|
    > unless fl == '.' or fl == '..' then
    > puts my_path + fl
    > File.open(my_path + fl, "r").each do |line|
    > if line =~ /error/ then
    > error += 1
    > elsif line =~ /warning/ then
    > warning += 1
    > end
    > end
    >
    > end
    > end
    > puts warning.to_s
    > puts error.to_s
    >
    > The problem that I have now is that it apparently sees the file but does
    > not read it.
    >
    > Below is the response that is returned.
    >
    > C:\Ruby\Text\log_file_20090221_041817.txt
    > 0
    > 0
    > Press ENTER to close the window...
    >


    Without seeing your file I can't offer any specific advice. If it was me
    I'd add a "puts line" as the first statement in the block, just to make
    sure that the value of line is what I expect it to be.

    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Mar 1, 2009
    #5
  6. Brad Winborg

    Tim Greer Guest

    Brad Winborg wrote:

    > Tim Hunter wrote:
    >> Brad Winborg wrote:
    >>> error = 0
    >>> end
    >>> Not a directory - D:\My
    >>> Documents\Replicon\log_file_20090221_041817.txt\ D:/My
    >>> Documents/Replicon/ErrorCount.rb:6:in `initialize' Press ENTER to
    >>> close the window...
    >>>
    >>> Directory Path
    >>> D:\My Documents\Replicon
    >>> I was going to add an attachment to show that the directory really
    >>> exits and that the file is in that directory, but it was to large.
    >>>
    >>> If anyone has any ideas as to why it is not working I be very
    >>> grateful, as I am new to ruby code in general.

    >>
    >> Look at the error message. You're asking Dir.new to open the
    >> directory "D:\My Documents\Replicon\log_file_20090221_041817.txt".
    >> I think the directory name is actually "D:\My Documents\Replicon" and
    >> "log_file_20090221_041817.txt" is a file within that directory.

    >
    > Thank you that did help, but I still have a problem.
    >
    > my_path = "C:\\Ruby\\Text\\"
    >
    > warning = 0
    > error = 0
    >
    > d = Dir.new(my_path)
    > d.each do |fl|
    > unless fl == '.' or fl == '..' then
    > puts my_path + fl
    > File.open(my_path + fl, "r").each do |line|
    > if line =~ /error/ then
    > error += 1
    > elsif line =~ /warning/ then
    > warning += 1
    > end
    > end
    >
    > end
    > end
    > puts warning.to_s
    > puts error.to_s
    >
    > The problem that I have now is that it apparently sees the file but
    > does not read it.
    >
    > Below is the response that is returned.
    >
    > C:\Ruby\Text\log_file_20090221_041817.txt
    > 0
    > 0
    > Press ENTER to close the window...
    >


    That code should work. Are you sure that the text file's content
    actually match "error" or "warning"? Did you check the letter case?
    Perhaps try and put a "puts line" right after the open and see what it
    outputs. It looks to be reading the files anyway, otherwise you'd see
    an error, rather than just the initial variable's value of 0.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Mar 1, 2009
    #6
  7. Brad Winborg

    Brad Winborg Guest

    Tim Greer wrote:
    > Brad Winborg wrote:
    >
    >>>> D:\My Documents\Replicon

    >>
    >> puts my_path + fl
    >> puts warning.to_s
    >> Press ENTER to close the window...
    >>

    >
    > That code should work. Are you sure that the text file's content
    > actually match "error" or "warning"? Did you check the letter case?
    > Perhaps try and put a "puts line" right after the open and see what it
    > outputs. It looks to be reading the files anyway, otherwise you'd see
    > an error, rather than just the initial variable's value of 0.


    I did check the case and it was wrong. I changed it to match and it made
    a difference. I still don't think its working correctly. If your curious
    I have attached the text file that it is reading.

    my_path = "C:\\Ruby\\Text\\"

    WARNING = 0
    ERROR = 0

    d = Dir.new(my_path)

    d.each do |fl|
    unless fl == '.' or fl == '..' then
    puts my_path + fl
    File.open(my_path + fl, "r").each do |line|
    if line =~ /ERROR/ then
    error += 1
    elsif line =~ /WARNING/ then
    warning += 1
    end
    end

    end
    end
    puts warning.to_s
    puts error.to_s

    Attachments:
    http://www.ruby-forum.com/attachment/3372/log_file_20090221_041817.rar

    --
    Posted via http://www.ruby-forum.com/.
    Brad Winborg, Mar 2, 2009
    #7
  8. Brad Winborg

    Phlip Guest

    Brad Winborg wrote:

    > I did check the case and it was wrong. I changed it to match and it made
    > a difference. I still don't think its working correctly. If your curious
    > I have attached the text file that it is reading.
    >
    > my_path = "C:\\Ruby\\Text\\"


    Paths are much easier to manage using / , even on Windows.

    > WARNING = 0
    > ERROR = 0


    These are not the same as warning and error. And this...

    warnings = 0
    errors = 0

    ....provides better "ear" - better hints that we are counting things.

    > d = Dir.new(my_path)
    >
    > d.each do |fl|
    > unless fl == '.' or fl == '..' then


    Only use unless in a pinch. This is better style:

    if fl != '.' and fl != '..'

    > puts my_path + fl
    > File.open(my_path + fl, "r").each do |line|


    Try File.readlines(my_path + fl).each do |line|

    > if line =~ /ERROR/ then
    > error += 1


    You don't need 'then'. It's allowed, but it makes the code more verbose.

    > elsif line =~ /WARNING/ then
    > warning += 1
    > end
    > end
    >
    > end
    > end
    > puts warning.to_s
    > puts error.to_s


    puts implies .to_s. Try:

    puts "Warnings: #{warnings}; Errors: #{errors}"

    Now, would this work?

    warnings = File.readlines(my_path + fl).grep(/WARNING/).length

    You would be amazed what Ruby can pack into one statement!

    --
    Phlip
    Phlip, Mar 2, 2009
    #8
  9. Brad Winborg

    Brad Winborg Guest

    Phlip wrote:
    > Brad Winborg wrote:
    >
    >> I did check the case and it was wrong. I changed it to match and it made
    >> a difference. I still don't think its working correctly. If your curious
    >> I have attached the text file that it is reading.
    >>
    >> my_path = "C:\\Ruby\\Text\\"

    >
    > Paths are much easier to manage using / , even on Windows.
    >
    >> WARNING = 0
    >> ERROR = 0

    >
    > These are not the same as warning and error. And this...
    >
    > warnings = 0
    > errors = 0
    >
    > ...provides better "ear" - better hints that we are counting things.
    >
    >> d = Dir.new(my_path)
    >>
    >> d.each do |fl|
    >> unless fl == '.' or fl == '..' then

    >
    > Only use unless in a pinch. This is better style:
    >
    > if fl != '.' and fl != '..'
    >
    >> puts my_path + fl
    >> File.open(my_path + fl, "r").each do |line|

    >
    > Try File.readlines(my_path + fl).each do |line|
    >
    >> if line =~ /ERROR/ then
    >> error += 1

    >
    > You don't need 'then'. It's allowed, but it makes the code more verbose.
    >
    >> elsif line =~ /WARNING/ then
    >> warning += 1
    >> end
    >> end
    >>
    >> end
    >> end
    >> puts warning.to_s
    >> puts error.to_s

    >
    > puts implies .to_s. Try:
    >
    > puts "Warnings: #{warnings}; Errors: #{errors}"
    >
    > Now, would this work?
    >
    > warnings = File.readlines(my_path + fl).grep(/WARNING/).length
    >
    > You would be amazed what Ruby can pack into one statement!


    I am feeling a bit stupid at this point parts of what you have said seem
    to make sense but other parts don't. I don't to keep bothering you but I
    really want to understand what is happening and I am not sure what you
    are telling me to replace. You mention warnings allot but not error. I
    have replaced what I believe you were saying. whatever you have been a
    tremendous help and I hope one gay I can return the favor, but all this
    is new stuff to me. I have read allot and it has not helped so far that
    is why I have turned to forums like this because I can actually interact
    with real people. Below is the code I changed to what I thought you were
    telling me.

    my_path = "C:/Ruby/Text/"

    warnings = 0
    errors = 0

    d = Dir.new(my_path)

    if fl != '.' and fl != '..'

    File.readlines(my_path + fl).each do |line|

    if line =~ /ERROR/
    error += 1
    elsif line =~ /WARNING/
    warning += 1
    end
    end

    end
    end

    puts implies .to_s. Try:

    puts "Warnings: #{warnings}; Errors: #{errors}"

    warnings = File.readlines(my_path + fl).grep(/WARNING/).length

    puts error.to_s

    --
    Posted via http://www.ruby-forum.com/.
    Brad Winborg, Mar 2, 2009
    #9
  10. Brad Winborg

    lasitha Guest

    On Mon, Mar 2, 2009 at 8:53 AM, Phlip <> wrote:
    > Brad Winborg wrote:
    >>
    >> d.each do |fl|
    >> =A0 unless fl =3D=3D '.' or fl =3D=3D '..' then

    >
    > Only use unless in a pinch. This is better style:
    > =A0 =A0if fl !=3D '.' and fl !=3D '..'


    I usually agree with anything Phlip says, but not on this :)

    I love that ruby has the unless keyword and use it often. IMO the
    version with unless above reads just fine and you should go with
    whichever reads better for you.

    No need to open up a conversation about unless vs. if !, particularly
    since it's happened before [1]. Just wanted Brad to hear an
    alternative opinion.

    Solidarity,
    lasitha

    [1] e.g. http://markmail.org/thread/4ywej4tsqhe47b3a
    lasitha, Mar 2, 2009
    #10
    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. Benjamin Rutt
    Replies:
    2
    Views:
    931
    Peter Hansen
    Dec 20, 2005
  2. Rajat
    Replies:
    3
    Views:
    689
    Jorgen Grahn
    Jan 8, 2010
  3. Replies:
    2
    Views:
    147
    Sean O'Halpin
    Dec 17, 2008
  4. Replies:
    4
    Views:
    183
    Josef Moellers
    Oct 27, 2007
  5. Replies:
    4
    Views:
    292
    ccc31807
    Jul 29, 2011
Loading...

Share This Page