From: nobu.nokada@... Date: 2004-09-02T09:41:43+09:00 Subject: Re: Searching Directories Hi, At Thu, 2 Sep 2004 04:50:22 +0900, Jabari Zakiya wrote in [ruby-talk:111197]: > 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 Don't you want to check if it is a file? Many file systems have other types than file and directory, you should use File.file?(entry) or: stat = File.stat(entry) if stat.file? > # 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}/ Compiling regexp each time would be too expensive. > # 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 Leaving opened files is very bad manner. You can use File.mtime(entry) instead, or with above File.stat: out.puts(name+ ", " + entry + ", " + stat.mtime.to_s) puts name + ", " + entry + ", " + stat.mtime.to_s -- Nobu Nakada