shandor has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I search a directory tree for files?
by Anonymous Monk on Jun 28, 2000 at 12:28 UTC | |
There are two possibilities, depending on how static the directory contents are, and how willing you are to trade speed against memory. The first solution searches the whole tree. This is the solution to go for if the directories themselves are static, but the contents of the directories are not static. This version is slow, but it dosen't consume much memory (on harddisk).
The second version uses a two step approach. We compute a list of all (interesting) files in the directory tree once, and save it into a file. If we want to check if a certain file is in the directory tree, we load this file into a hash and have a really fast lookup (if we want to look up more than one file) or we go through the file line by line (if we only look for a single file). This method obviously only works if the directory contents don't change very often, because our file is not always up-to-date. The code above serves very well to create the list of interesting files, just redirect its output into a file called index.
| [reply] [d/l] [select] |