Q&A > directories
|Answer: How do I search a directory tree for files?|
contributed by QandAEditors
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.
Please (register and) log in if you wish to add an answer
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr