File size vs. Directory size problem

Discussion in 'Ruby' started by T.w.oliver@gmail.com Tom, Nov 10, 2009.

  1.  Tom

    Tom Guest

    Hi all,
    Got a bit of a problem I dont know how to go about.
    Basically my script takes 2 different directories, puts each file/sub
    directory into an array for each, sorts the array, compares them, and
    then spits out what is missing from each directory. Im really happy with
    this as it is my first real ruby script and it works a charm.

    Now I was wanting to add to it. Basically I want to take the size of
    each file and see if they are greater than 0, and if it is 0 then output
    the file.

    My problem is that a directory is returned as being of size 0. Obviously
    I dont care about the directory size in this case, only the file size.
    How can i write my script to that it ignores the directory and only
    checks if it is a file.

    So at the moment my code goes
    Dir.chdir(old)
    old_array = Array.new
    for i in Dir['**/**']
    #puts i
    puts i
    puts File.size(i)
    old_array << i
    end

    The puts File.size(i) is there for a semi test, and it shows me that
    directories are returned with size 0.

    Any thoughts?

    Thanks so much :D
    --
    Posted via http://www.ruby-forum.com/.
    Tom, Nov 10, 2009
    #1
    1. Advertising

  2.  Tom

    Sven Schott Guest

    [Note: parts of this message were removed to make it a legal post.]

    Maybe something like:

    puts File.size(i) unless File.directory?(i)

    On Tue, Nov 10, 2009 at 4:47 PM, Tom <
    > wrote:

    > Hi all,
    > Got a bit of a problem I dont know how to go about.
    > Basically my script takes 2 different directories, puts each file/sub
    > directory into an array for each, sorts the array, compares them, and
    > then spits out what is missing from each directory. Im really happy with
    > this as it is my first real ruby script and it works a charm.
    >
    > Now I was wanting to add to it. Basically I want to take the size of
    > each file and see if they are greater than 0, and if it is 0 then output
    > the file.
    >
    > My problem is that a directory is returned as being of size 0. Obviously
    > I dont care about the directory size in this case, only the file size.
    > How can i write my script to that it ignores the directory and only
    > checks if it is a file.
    >
    > So at the moment my code goes
    > Dir.chdir(old)
    > old_array = Array.new
    > for i in Dir['**/**']
    > #puts i
    > puts i
    > puts File.size(i)
    > old_array << i
    > end
    >
    > The puts File.size(i) is there for a semi test, and it shows me that
    > directories are returned with size 0.
    >
    > Any thoughts?
    >
    > Thanks so much :D
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Sven Schott, Nov 10, 2009
    #2
    1. Advertising

  3.  Tom

    Tom Tom Guest

    Sven Schott wrote:
    > Maybe something like:
    >
    > puts File.size(i) unless File.directory?(i)
    >
    > On Tue, Nov 10, 2009 at 4:47 PM, Tom <


    I completely missed the directory method haha. Thank you sir.
    --
    Posted via http://www.ruby-forum.com/.
    Tom Tom, Nov 10, 2009
    #3
  4. 2009/11/10 Tom Tom <>:
    > Sven Schott wrote:
    >> Maybe something like:
    >>
    >> puts File.size(i) unless File.directory?(i)
    >>
    >> On Tue, Nov 10, 2009 at 4:47 PM, Tom <

    >
    > I completely missed the directory method haha. Thank you sir.


    There is also the not so well known method "test" which can be used for this:

    old_array = Dir["#{old}/**/*"]
    old_array.each do |f|
    puts File.size(f) unless test ?d, f
    end

    Btw, the original code copied the result of Dir[] into another Array.
    That seems unnecessary additional work. You can directly use the
    result of Dir[].

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 11, 2009
    #4
    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. tiewknvc9
    Replies:
    6
    Views:
    638
    Chris Uppal
    Oct 1, 2006
  2. Keith Thompson

    Re: File Size - Big File Size

    Keith Thompson, Oct 1, 2009, in forum: C Programming
    Replies:
    6
    Views:
    274
    Phil Carmody
    Oct 3, 2009
  3. Michael Tsang

    Re: File Size - Big File Size

    Michael Tsang, Oct 4, 2009, in forum: C Programming
    Replies:
    2
    Views:
    305
    Keith Thompson
    Oct 4, 2009
  4. Nobody

    Re: File Size - Big File Size

    Nobody, Oct 5, 2009, in forum: C Programming
    Replies:
    10
    Views:
    1,995
    Flash Gordon
    Oct 10, 2009
  5. John Joyce
    Replies:
    2
    Views:
    129
    John Joyce
    Apr 27, 2007
Loading...

Share This Page