From: Osuka Adartse Date: 2004-09-02T09:45:44+09:00 Subject: Re: Searching Directories Jabari Zakiya wrote: > > 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 unless I didn't understood You, there's no real point for this 2 lines... so I changed'em to deleting also the next 4... if !FileTest.directory?(entry) && exts.include?(File.extname(entry)) > # Else entry was a file > else > # If current file has a desired extension > if exts.include? File.extname(entry) I prefer to add the ()'s for readability, I wondered for a sec' what the above line meant ;-) > # 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) btw there's no need to open the file for using several of File.methods or the to_s > puts name + ", " + entry + ", " + File.open(entry).mtime.to_s a matter of preference but I avoid: puts variable + ", " + ...etc intead puts "#{variable}, #{var2}" or better use printf to get more control on output, it's easier on the eyes...at least for me i.e. printf("%-24s, %-48s , %s\n",name,entry,File.mtime(entry)) I used strftime for similar reasons. > total += 1 > end > } > end > end > } > print "Total files = ", total , "\n" > out.close > end > > goodie!! :-) 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