[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Create a directories list with sub-directories
From:       Vadim Nasardinov <vadimn () redhat ! com>
Date:       2004-03-24 18:32:18
Message-ID: 200403241336.50319 () vadim ! nasardinov
[Download RAW message or body]

On Wednesday 24 March 2004 03:44, Dirk Einecke wrote:
> X-Mail-Count: 95709
> Message-Id: <c3rhhs$2bfft5$1@ID-43561.news.uni-berlin.de>
>
> I have one question: With your code the files in a folder will be
> print out at first. Is it possible to change this that the order is
> inverted: at first the folders an after that the files?

#!/usr/bin/ruby

WSIndent    = "   "
LinedIndent = "|  "
FileIndent   = "+- "

def filetree(dir, indent)
    files = []
    dirs =  []

    Dir.foreach(dir) do |ff|
        path = File.join(dir, ff)
        if File.directory?(path)
            dirs.push(ff) if  ff[0] != ?.
        else
            files.push(ff)
        end
    end

    if dirs.length > 0
        dirs.sort!

        lastDirSpecial = false
        dirRange = 0..0
        if files.length > 0
            dirRange = 0..-1            
        elsif dirs.length > 1
            dirRange = 0..-2
            lastDirSpecial = true
        end

        nextIndent = indent + LinedIndent
        thisIndent = indent + FileIndent
        dirs[dirRange].each do |dd|
            puts "#{thisIndent}#{dd}/"
            filetree(File.join(dir, dd), nextIndent)
        end

        if lastDirSpecial
            puts "#{indent + FileIndent}#{dirs[-1]}/"
            filetree(File.join(dir, dirs[-1]), indent + WSIndent)
        end
    end

    files.sort!
    fileIndent = indent + FileIndent
    files.each do |ff|
        puts "#{fileIndent}#{ff}"
    end
end

ARGV.each do |dd|
    puts "+#{dd}"
    filetree(dd, "")
end


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic