# Regex parsing against filenames

**URL:** <https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030>\
**Category:** Ruby\
**Created:** [October 13, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030 "2006-10-13T18:41:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [October 13, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030/1 "2006-10-13T18:41:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [October 13, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030/2 "2006-10-13T19:30:42Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [October 13, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030/3 "2006-10-13T21:02:30Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [October 13, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/regex-parsing-against-filenames/72030/4 "2006-10-13T20:31:03Z")

</div>

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  
removed\_email\_address@domain.invalid -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
