From: jzakiya@... (Jabari Zakiya) Date: 2004-09-02T04:50:22+09:00 Subject: Re: Searching Directories Osuka Adartse wrote in message news:<4133BC0C.3020902@prodigy.net.mx>... > 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 > > > > > require 'find' > dirs=Array.new > files=Array.new > Find.find('/directory/path'){|entry| > if FileTest.directory?(entry) > dirs.push(entry) > else > files.push(entry) > end > } > > #by now dirs and files are arrays of diretories and files, find is > #recursive so You can use it instead of Dir[] > require 'pp' > pp dirs > #=>["E:/usr\\local/rdoc/sqlite-1.3.0", > "E:/usr\\local/rdoc/sqlite-1.3.0/files", > "E:/usr\\local/rdoc/sqlite-1.3.0/classes", > "E:/usr\\local/rdoc/sqlite-1.3.0/classes/SQLite"] > #process files... > files.each{|f| > case File.extname(f) > when '.html' then puts f + ' : is a HTML' > when 'rid' then puts f + ' : is mmmh something...' > end > } > #results in > #=> > E:/usr\local/rdoc/sqlite-1.3.0/index.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/fr_method_index.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/fr_file_index.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/fr_class_index.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/files/sqlite_rb.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/files/sqlite_c.html : is a HTML > E:/usr\local/rdoc/sqlite-1.3.0/created.rid : is mmmh something... > > hope this may help you... > Adartse Thanks for the suggestions. Heres my approach. --------------------------------------------- require 'find' def dirsearch(namefile, topdir, outfile) out = File.new(outfile, "w") #Total number of files found total = 0 # Array with file extensions to check for exts = %w{.doc .rtf .txt} # Check entries in each subdirectory of TopDir Find.find(topdir){|entry| # If entry a Directory search inside it if FileTest.directory?(entry) next # Else entry was a file else # If current file has a desired extension if exts.include? File.extname(entry) # Check for each name in namefile File.open(namefile).each{|name| name = name.chomp # If name is part of basename of file if File.basename(entry) =~ /#{name}/ # Write line to output file and screen if there is a match out.puts(name+ ", " + entry + ", " + File.open(entry).mtime.to_s) puts name + ", " + entry + ", " + File.open(entry).mtime.to_s total += 1 end } end end } print "Total files = ", total , "\n" out.close end