[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regex parsing against filenames

Peter Bailey

10/13/2006 4:42:00 PM

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

--
Posted via http://www.ruby-....

3 Answers

Hugh Sasse

10/13/2006 5:30:00 PM

0

Peter Bailey

10/13/2006 6:31:00 PM

0

Hugh Sasse wrote:
> On Sat, 14 Oct 2006, Peter Bailey 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
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

--
Posted via http://www.ruby-....

Hugh Sasse

10/13/2006 7:02:00 PM

0