Recursively listing files into directories

A

Alain.Feler

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
 
A

Alain FELER

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 ...).
 
G

Ghislain Mary

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
(e-mail address removed) with a subject of:
subscribe Alain Feler

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

Ghislain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top