How do I build a tree of directories?

Discussion in 'Ruby' started by Ben Knight, Jul 24, 2007.

  1. Ben Knight

    Ben Knight Guest

    Hello. I need to send XML back to a client app listing all my
    directories and sub-directories on the server. The XML will look
    something like this:

    <folders>
    <folder name="directory-name">
    <folder name="directory name"
    <folder name="directory name">
    </folder>
    </folder>
    </folder>
    </folders>

    I'm doing this in Rails. I have a couple of questions:

    1. In my controller, how do I use something like Find.find(path) to do
    this effectively? For example, do I need to make recursive calls to a
    method for each subdirectory encountered? Do I build an array of arrays
    for the sub-directories? A Hash?

    2. Once my array of arrays or hash or whatever is built, what's the best
    way to output this in my view?

    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
    Ben Knight, Jul 24, 2007
    #1
    1. Advertising

  2. On Jul 24, 3:06 pm, Ben Knight <> wrote:
    > Hello. I need to send XML back to a client app listing all my
    > directories and sub-directories on the server. The XML will look
    > something like this:
    >
    > <folders>
    > <folder name="directory-name">
    > <folder name="directory name"
    > <folder name="directory name">
    > </folder>
    > </folder>
    > </folder>
    > </folders>
    >
    > I'm doing this in Rails. I have a couple of questions:


    > 1. In my controller, how do I use something like Find.find(path) to do
    > this effectively? For example, do I need to make recursive calls to a
    > method for each subdirectory encountered?


    You'd want to use one of the Dir methods; foreach(), glob()...

    Entry = Struct.new:)dir,:children)

    def recurse(path)

    entry = Entry.new(path,[])

    #no "." or ".." dirs
    Dir["#{path}/*"].each do |e|
    if File.directory?(e)
    entry.children << recurse(e)
    end
    end

    entry

    end

    > Do I build an array of arrays for the sub-directories? A Hash?


    Depends. If you just need the directory name and its children, then an
    array of arrays will work. If you need additional information, then
    use a Hash. Maybe a Struct (for fun).

    > 2. Once my array of arrays or hash or whatever is built, what's the best
    > way to output this in my view?


    XML Builder, left as an exercise.

    If you do not have additional requirements for the directory data,
    i.e. you just want to output it as XML, then I'd say write the XML
    with XML Builder as you recurse. Especially if your directory
    structure is big.
    Skye Shaw!@#$, Jul 25, 2007
    #2
    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. Joel Finkel
    Replies:
    0
    Views:
    488
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    522
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    823
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,109
  5. Paul F
    Replies:
    4
    Views:
    629
    Mark Fitzpatrick
    Jun 25, 2008
Loading...

Share This Page