From: jzakiya@... (Jabari Zakiya) Date: 2004-09-03T03:25:19+09:00 Subject: Re: Searching Directories Osuka Adartse wrote in message news:<4136971B.8090208@prodigy.net.mx>... [...] > my lil' changes FWIW > > def dirsearch(namefile, topdir, outfile) > require 'find' > 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| > # we're looking for files not dirs and files thta match exts so... > if !FileTest.directory?(entry) && 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.printf("%-16s: %-48s: > %s\n",name,entry,File.mtime(entry).strftime("%d-%b-%Y %H:%M")) > #either this line with mtime's output as it is or formatted > with strftime > #strftime makes things more my way omitting info I don't > need/want... > #option#printf("%-24s: %-48s: > %s\n",name,entry,File.mtime(entry)) > printf("%-16s: %-48s: > %s\n",name,entry,File.mtime(entry).strftime("%d-%b-%Y %H:%M")) > total += 1 > end > } > end > } > print "Total files = #{total} \n" > out.close > end > > > cheers > Adartse --------------------------------------------------------- Taking everyones suggestions into account here is my new version. This version should be more amenable to different systems. I didn't bother to do elaborate output formatting because the person who needs the output is satisfied with the current format. I will use the formatting suggestions if I need to in the future. There also is a default output file. One thing I DO NEED is help on is EXCEPTION HANDLING! I encountered CORRUPTED directories on a disk I searched through, which caused the program to bomb with an error message. I would like suggestions on including handling DIR/FILE EXCEPTIONS. Ideally, I would like to be able to continue searching when EXCEPTIONS are met and record what the EXCEPTION (corrupted DIR/FILE) was. Is this possible? Nobu, I didn't understand your comment about the Regexp searching. Could you explain in more detail the issue, and an alternative approach. Also, I could have first put all the names in 'namefile' in an array, and then iterate over that, for purposes of speed, but in this case it doesn't matter. I might try it though, just to see if it speeds things up appreciably. Again, thanks is advance for your help and suggestions! ;-) --------------------------------------------------------- require 'find' def dirsearch(namefile, topdir, outputfile='searchresults.txt') outfile = File.new(outputfile, "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 is a file with a desired extension if File.file?(entry) && 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 outfile.puts("#{name}:\n #{entry}, #{File.stat(entry).mtime.to_s}") puts "#{name}:\n #{entry}, #{File.stat(entry).mtime.to_s}" total += 1 end } # Else get next entry else next end } print "Total files = ", total , "\n" outfile.close end