Regex parsing against filenames

Hi,
I need to match incoming filenames in a directory on my server, so that
I can do stuff in a table. In my testing, I’ve got 9 files:

pq181b1p.ps
pq181b1t.ps
pq181b2p.ps
pq181b2t.ps
pq181b3t.ps
pq192b1p.ps
pq192b1t.ps
pq192b2t.ps
pq192b3p.ps

Only filenames 1,2,6 and 7 parse against the entry “dbfilename” in my
database. The other 5 don’t. The entries, for these files, in the
database are “pq181b” and “pq192b.” Shouldn’t all 9 of these filenames
parse true against “pq181b” and “pq192b??”

Here’s a snippet of my code, showing where I’m doing the regex. All the
filenames are in the variable $psfile.


db = KirbyBase.new { |d| d.path = “E:/workflows/graphics/pagecounts” }
pageinfo_tbl = db.get_table(:pageinfo)

result = pageinfo_tbl.select { |r| $psfile =~ Regexp.new(r.dbfilename) }

Thanks,
Peter

On Sat, 14 Oct 2006, Peter B. wrote:

parse true against “pq181b” and “pq192b??”

Here’s a snippet of my code, showing where I’m doing the regex. All the
filenames are in the variable $psfile.


db = KirbyBase.new { |d| d.path = “E:/workflows/graphics/pagecounts” }
pageinfo_tbl = db.get_table(:pageinfo)

result = pageinfo_tbl.select { |r| $psfile =~ Regexp.new(r.dbfilename) }

Without seeing the result of the r.dbfilename call it’s hard to tell.

result = pageinfo_tbl.select do |r|
dbf = r.dbfilename
re = Regexp.new(dbf)

if re.match($psfile)
  true
else
  puts "#{r}.dbfilename => #{dbf} => #{re} which failed to match 

#{$psfile}"
false
end
end

might tell you something.

Thanks,
Peter

    Hugh

On Sat, 14 Oct 2006, Peter B. wrote:

Hugh S. 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.
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

end
File.rename($psfile, $psfile.downcase)
end

    Hugh

Hugh S. wrote:

On Sat, 14 Oct 2006, Peter B. wrote:

parse true against “pq181b” and “pq192b??”

Here’s a snippet of my code, showing where I’m doing the regex. All the
filenames are in the variable $psfile.


db = KirbyBase.new { |d| d.path = “E:/workflows/graphics/pagecounts” }
pageinfo_tbl = db.get_table(:pageinfo)

result = pageinfo_tbl.select { |r| $psfile =~ Regexp.new(r.dbfilename) }

Without seeing the result of the r.dbfilename call it’s hard to tell.

result = pageinfo_tbl.select do |r|
dbf = r.dbfilename
re = Regexp.new(dbf)

if re.match($psfile)
  true
else
  puts "#{r}.dbfilename => #{dbf} => #{re} which failed to match 

#{$psfile}"
false
end
end

might tell you something.

Thanks,
Peter

    Hugh

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.
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!

Here’s my whole script now. I hope it’s not too huge for this forum.

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) }
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
[email protected] -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