Dir, recursive filescan

Hi,

i want to count the occurence of a searchstring in all files
below a directory and all its subdirectories

i have =

matches=0
Dir.glob(“E:/test/foobar/scripts//.xml”).each do |f|
matches += File.new(f).read.scan(’’).size
puts f
end

puts "Matchcount == " + matches.to_s
puts "Scanned Files == " +
Dir.glob(“E:/test/foobar/scripts//.xml”).size.to_s

Is there a better way ?

Regards,
Gilbert

On 24.08.2007 16:19, Rebhan, Gilbert wrote:

puts "Matchcount == " + matches.to_s
puts "Scanned Files == " +
Dir.glob(“E:/test/foobar/scripts//.xml”).size.to_s

Is there a better way ?

Yes, you should at least avoid globbing twice. This is really slow.
Also, you do not need two stars for the file name.

Then, you do not close files properly when reading. A simplified
version looks like this

files = Dir[“E:/test/foobar/scripts/**/*.xml”]
matches = files.inject(0) {|sum,f| sum + File.read(f).scan(’’).size}
printf “%10d matches\n%10d files\n”, matches, files.size

If you have a lot of files it may pay off to use Find.find instead of
Dir[].

Kind regards

robert

Hi, Robert

2007/8/27, Rebhan, Gilbert [email protected]:

matches = files.inject(0) {|sum,f| sum +
/*

I think that’s the reason you mentioned to use Find.find yor large
dirtrees ?!

Not exactly. My reasoning was that the Dir[] returns a large array
while Find invokes the block once per file found.

But your problem is related to error handling. Dunno what Find does
here but the root cause of your issue is that you seem to not have
permissions for one of the directories.

Kind regards

robert

2007/8/27, Rebhan, Gilbert [email protected]:

files = Dir[“E:/test/foobar/scripts/**/*.xml”]
matches = files.inject(0) {|sum,f| sum + File.read(f).scan(’’).size}
printf “%10d matches\n%10d files\n”, matches, files.size

If you have a lot of files it may pay off to use Find.find instead of
Dir[].
*/

thanks, works like a charm :slight_smile:
Never used the inject method until now, i’m still on newbie level.

Took me a while, too. But if you get the hang of it you’ll see it’s a
really cool utility. At one point I rewrote all the Enumerable
methods via #inject just for the learning experience. :slight_smile:

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs