From: Markus Date: 2004-08-31T04:02:38+09:00 Subject: Re: Searching Directories Pseudo-code is your friend. What you say you want to do is something like: define find_stuff_in a_directory for each thing in a_directory if it is a directory find stuff in it (unless it's . & ..) else (when it's a file) print information about it if its interesting All of this is from you e-mail, and could apply to any language. Now for the ruby part. Look up the class Dir, which will give you the contents of a directory as an array, Array (and Enumerable) which let you walk through instances, and Regexp which let you test strings to see if they match patterns. Note that the recursion sort of takes care of itself in the forth line of the pseudo code. Have fun. -- MarkusQ On Mon, 2004-08-30 at 11:40, Jabari Zakiya wrote: > I'm trying to figure out the best way to accomplish this task. > > Being new to Ruby, I'm trying to be efficient (if not elegant), > but first just want something that works. > > I'm using Ruby 1.8.2 rc4 on Windows 2000. > > Here's the task. > > -------------- > I have text files of lastnames, one name per line. > > I want to read each name from each file, and determine > if that name is part of any file which has the extensions > *.txt or *.doc or *.rtf which occur in any directory within > the tree structure for a given top level directory. > > The human interface is something simple like this: > > The text filename is keyboard entered into "NameListFile". > The top-level-dir is keyboard entered into "DirToSearch". > The output-file is keyboard entered into "SearchResults". > > Doing something like: DirItems = Dir.entries(DirToSearch) > I can get an array of items in the top-level directory. > > Given this array of files and directories, I just need to > check to see if an item is a "file" or "directory". > > If its a "file" I check to see that its extension is > *.txt or *.doc or *.rtf. If the file has any of those > extensions, I then check to see whether the filename > contains, in any part, a name from "NameListFile". > > If YES, I print the name in an output file in the formant: > Name, filedate, filename(fullpath relative to "DirToSearch") > > If an item in DirItems is a "directory" then I do the checking > process for the entries in that directory, etc, etc, untill > every file in every directory is searched. > > It seems this is a natural case to use recursion to check all > the directories in the tree, but I'm not sure which methods > to use to do this. > > This seems like a pretty easy/standard thing to do, but I just > don't know enough ruby (yet) to do it easy. > > Your help and guidance is appreciated in advance. > > Jabari