Many of our customers want to learn how to traverse all the files in a directory, or perhaps even all the files in
or below a directory. I quite often write a demonstration program during our courses which looks though part of a file system tree for files over a certain size, or for the largest (so many) files. It's a neat and useful demosntration of an application which most people can identify with.
See examples in
Ruby,
Tcl and
Perl.
The method used is to keep a queue of directories to be checked in a list / array, and travese them one at a time. As new directories are found, they're added onto the end of the list / array, and as each directory is traversed, it's removed from the array, And when the array is empty, the program is done.
It's been suggested to me that when a directory is found within another ditrectory, the traversing of the parent should be suspended while the child (and any children it has) are handled. That can be done neatly through recurrsion, but the code ends up being a bit too "clever" to be easy to follow and - unless you need to traverse in a certain order - it's unnecessary.
(written 2011-09-09)
Associated topics are indexed under
R106 - Input and Output in Ruby [2974] Formatting your output - options available in Ruby - (2010-09-29)
[2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2614] Neatly formatting results into a table - (2010-02-01)
[2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1587] Some Ruby programming examples from our course - (2008-03-21)
T209 - Tcl/Tk - File and Directory Handling [3617] The fileutil package and a list of file system commands in Tcl - (2012-02-18)
[3320] Reading the nth line from a file (Perl and Tcl examples) - (2011-06-09)
[3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
[1426] Buffering up in Tcl - the empty coke can comparison - (2007-11-10)
[1407] Reading from another process in Tcl (pipes and sockets) - (2007-10-26)
[785] Running external processes in Tcl and Tcl/Tk - (2006-06-29)
[779] The fragility of pancakes - and better structures - (2006-06-26)
P602 - Perl - Advanced File and Directory Handling [3412] Handling binary data in Perl is easy! - (2011-08-30)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
[1832] Processing all files in a directory - Perl - (2008-10-11)
[1709] There is more that one way - Perl - (2008-07-14)
[1225] Perl - functions for directory handling - (2007-06-09)
[975] Answering ALL the delegate's Perl questions - (2006-12-09)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
Some other Articles
Exceptions - a fail-safe way of trapping things that may go wrong3 digit HTTP status codes - what are they, which are most common, which should be a concern?Ruby at both extremes of your websiteSigils - the characters on the start of variable names in Perl, Ruby and FortranSearching through all the files in or below a directory - Ruby, Tcl, PerlHow many days to Christmas?What is on the Melksham Agenda?Automed web site testing scripted in Ruby using watir-webdriverOur National Autograss Champion, from MelkshamDivide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP