Recursively listing files into directories

Discussion in 'Ruby' started by Alain.Feler, May 15, 2005.

  1. Alain.Feler

    Alain.Feler Guest

    Hello,
    I want to load an array (not at tree) with all the paths and names of
    the files in a directory and its sub-directories.
    I achieve that with the following code, but I thing the $tabf is an ugly
    way to share the array between the recursive calls.
    Could somebody show me a better way ?
    # met en tableau la liste des fichiers (avec le chemin) d'un répertoire et
    de ses sous-répertoires, sans compter . et ..
    # loads the $tabf array with the files (and their paths) of the given
    directory and its sub-directories (omitting . and ..)

    $tabf = Array.new # the "ugly" shared array
    # the recursive procedure
    def fichiers_du_repertoire(repertoire)
    itabf = $tabf.size
    Dir.foreach(repertoire) do |entree|
    next if [".",".."].include? entree
    chemin_et_nom = repertoire + File::Separator + entree
    if FileTest::directory?(chemin_et_nom)
    fichiers_du_repertoire(chemin_et_nom)
    else
    $tabf[itabf] = chemin_et_nom
    itabf = itabf + 1
    end
    end
    end
    # the code wich uses the procedure
    rep = "c:/temp"
    fichiers_du_repertoire(rep)
    # shows the result
    print "Taille du tableau des fichiers:", $tabf.size.to_s, "\n"
    print "Size of the array of files :", $tabf.size.to_s, "\n"
    $tabf.each do |f|
    print f,"\n"
    end

    Thank you for your advices.
    AF
    Alain.Feler, May 15, 2005
    #1
    1. Advertising

  2. Alain.Feler

    dave Guest

    have you thought to use Find.find(directory) ?

    --
    >here are more things in heaven and earth,

    horatio, than are dreamt of in your philosophy.
    dave, May 15, 2005
    #2
    1. Advertising

  3. Alain.Feler

    Alain FELER Guest

    dave a écrit :
    >
    > have you thought to use Find.find(directory) ?
    >

    Thank you ! ... too much years of Cobol background (a <finite> language:
    if it is not in the keyword list, you must do it yourself ...).
    Alain FELER, May 15, 2005
    #3
  4. Hi Alain,

    If you'd like to ask your questions and have replies in french, you
    could try the french mailing list. To subscribe, just send a mail to
    with a subject of:
    subscribe Alain Feler

    Hope to see you ask questions on this list soon ;-)

    Ghislain
    Ghislain Mary, May 15, 2005
    #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. Ken
    Replies:
    1
    Views:
    3,733
    Roedy Green
    Dec 2, 2005
  2. Trinity
    Replies:
    1
    Views:
    505
    Peter van Merkerk
    Nov 7, 2003
  3. rbt
    Replies:
    5
    Views:
    8,972
  4. bahoo
    Replies:
    2
    Views:
    307
    Eli Criffield
    Apr 9, 2007
  5. albert kao
    Replies:
    5
    Views:
    675
    Walter Wefft
    May 16, 2010
Loading...

Share This Page