Script to walk a directory tree

Discussion in 'Ruby' started by Kyle Heon, Jul 26, 2005.

  1. Kyle Heon

    Kyle Heon Guest

    Can anyone point me in the direction of how to walk a directory tree
    recursively?

    I'm not having much luck finding code samples via Google.

    Thanks.

    Kyle Heon
    Kyle Heon, Jul 26, 2005
    #1
    1. Advertising

  2. Kyle Heon wrote:
    > Can anyone point me in the direction of how to walk a directory tree
    > recursively?
    >
    > I'm not having much luck finding code samples via Google.
    >
    > Thanks.
    >
    > Kyle Heon
    >
    >
    >
    >


    Check out 'find' in the standard library.

    http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/
    Timothy Hunter, Jul 26, 2005
    #2
    1. Advertising

  3. Kyle Heon

    Kirk Haines Guest

    On Monday 25 July 2005 5:22 pm, Kyle Heon wrote:
    > Can anyone point me in the direction of how to walk a directory tree
    > recursively?
    >
    > I'm not having much luck finding code samples via Google.


    def file_search(dirpath, file_regex)
    r = []
    Dir.foreach(dirpath) do |filename|
    next if filename == '.' or filename == '..'
    fullname = "#{dirpath}/#{filename}"
    if FileTest.directory? fullname
    r.concat(file_search(fullname))
    elsif file_regex.match(filename)
    r.push fullname
    end
    end
    r
    end

    That's quick and dirty, but should give you a starting place to develop what
    you want. Pass into file_search a starting path (with a trailing '/'), and a
    regex that will match what you are looking for. You'll get back and array
    with everything that was found to match.


    Kirk Haines
    Kirk Haines, Jul 26, 2005
    #3
  4. Kyle Heon

    Eric Hodel Guest

    On 25 Jul 2005, at 16:22, Kyle Heon wrote:

    > Can anyone point me in the direction of how to walk a directory tree
    > recursively?
    >
    > I'm not having much luck finding code samples via Google.


    require 'find'

    Find.find('/your/path') do |f|
    # if you want to skip all dirs
    next if File.directory? f
    puts "at: #{f}"
    end

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, Jul 26, 2005
    #4
  5. Kyle Heon

    Kirk Haines Guest

    On Monday 25 July 2005 5:33 pm, Eric Hodel wrote:
    > On 25 Jul 2005, at 16:22, Kyle Heon wrote:
    > > Can anyone point me in the direction of how to walk a directory tree
    > > recursively?
    > >
    > > I'm not having much luck finding code samples via Google.

    >
    > require 'find'
    >
    > Find.find('/your/path') do |f|
    > # if you want to skip all dirs
    > next if File.directory? f
    > puts "at: #{f}"
    > end


    Much better than implementing it manually, but the fun thing about manually is
    that it's not that much longer. Love Ruby.


    Kirk Haines
    Kirk Haines, Jul 26, 2005
    #5
  6. Kyle Heon

    Ara.T.Howard Guest

    On Tue, 26 Jul 2005, Kirk Haines wrote:

    > On Monday 25 July 2005 5:33 pm, Eric Hodel wrote:
    >> On 25 Jul 2005, at 16:22, Kyle Heon wrote:
    >>> Can anyone point me in the direction of how to walk a directory tree
    >>> recursively?
    >>>
    >>> I'm not having much luck finding code samples via Google.

    >>
    >> require 'find'
    >>
    >> Find.find('/your/path') do |f|
    >> # if you want to skip all dirs
    >> next if File.directory? f
    >> puts "at: #{f}"
    >> end

    >
    > Much better than implementing it manually, but the fun thing about manually is
    > that it's not that much longer. Love Ruby.


    it gets alot longer if you handle links, devices, potential infinite recursion
    due to directory links, and the fs changing underneath you causing errors to
    be thrown as often happens in the case of gigantic or nfs fs's... speaking
    from too much bad experience ;-)

    btw. the find2 module on c.l.r offers a find that follows links - something
    the built-in find does not do if this is important for the OP.

    cheers.

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | My religion is very simple. My religion is kindness.
    | --Tenzin Gyatso
    ===============================================================================
    Ara.T.Howard, Jul 26, 2005
    #6
  7. Hi,

    At Tue, 26 Jul 2005 08:22:34 +0900,
    Kyle Heon wrote in [ruby-talk:149485]:
    > Can anyone point me in the direction of how to walk a directory tree
    > recursively?


    Dir.glob("**/*") do |fn|
    # ...
    end

    --
    Nobu Nakada
    nobuyoshi nakada, Jul 26, 2005
    #7
    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. pembed2003

    how to walk a binary tree

    pembed2003, Apr 19, 2004, in forum: C++
    Replies:
    7
    Views:
    7,086
    pembed2003
    Apr 20, 2004
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,087
  3. ntfw (new file tree walk) question

    , Apr 6, 2005, in forum: C Programming
    Replies:
    7
    Views:
    394
    Lawrence Kirby
    Apr 6, 2005
  4. Joe Ardent
    Replies:
    4
    Views:
    351
    Gabriel Genellina
    May 28, 2007
  5. Marcus Alves Grando
    Replies:
    7
    Views:
    453
    Marcus Alves Grando
    Nov 14, 2007
Loading...

Share This Page