From: Hugh Sasse Date: 2006-10-14T04:02:16+09:00 Subject: Re: Regex parsing against filenames On Sat, 14 Oct 2006, Peter Bailey wrote: > Hugh Sasse wrote: > > Without seeing the result of the r.dbfilename call it's hard to tell. [...] > > might tell you something. [...] > > Thanks, Hugh. > I tried your code snipped and it gave me data for every single row in my > database, all 200 of them. So, it's kind of hard to read, because it > display every field of every row. So none matched. But did it tell you anything about the cases that didn't before, the unexpected failures? Maybe quote some of it? > I've tried to simply things by simply using the .match operator, like > this: > > result = pageinfo_tbl.select { |r| $psfile.match(r.dbfilename) } > > When I run this .match operation in IRB, using the 9 filenames I have, > it matches every time! So is that the correct answer -- it matches for all 9 that you want, or the incorrect answer -- it matches for more files than you want? > > Here's my whole script now. I hope it's not too huge for this forum. > re-indented. > > require 'kirbybase' > Dir.chdir("E:/workflows/graphics/indexes/VAXin_6x9") > psfiles = Dir.glob("*.ps") > psfiles.each do |$psfile| > File.rename($psfile, $psfile.downcase) > file_contents = File.read($psfile) > file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do > $totalpages = $1 > if ($totalpages.to_i % 2) !=0 then > file_contents << "\%\%Blank page from Asura\nshowpage\n" > File.open($psfile, "w") { |f| f.print file_contents } > $totalpages = $totalpages.to_i + 1 > end > db = KirbyBase.new { |d| d.path = "E:/workflows/graphics/pagecounts" } > pageinfo_tbl = db.get_table(:pageinfo) > result = pageinfo_tbl.select { |r| $psfile.match(r.dbfilename) } result = pageinfo_tbl.select do |r| $psfile.match(Regexp.new(r.dbfilename)) # The Regexp.new should ensure the filename is quoted correctly. end > if result.empty? #send an e-mail if empty > system("blat E:/live/scripts/messages/blank_page.txt -s \"#{$psfile} > is not > a known Index filename. Check with Accounting on this.\" -to > prodadmin@bna.com -f \"BNA Internal Production Mail Server\" -q") > else > $count = result.first.pagecount > puts "#{$psfile}, #{$count}, #{$totalpages}" #just for diagnostics > pageinfo_tbl.update(:pagecount=>$totalpages.to_i + $count.to_i) > { |r| r.recno == result.first.recno } > end > File.rename($psfile, $psfile.downcase) > end > Hugh