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
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