unknown wrote:
> On Wed, 27 Sep 2006, Peter Bailey wrote:
> 
>> 11    file_contents = File.read(psfile)
>> 12    file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do
> 
> the modification is probably here.  can't you show us everything up 
> through
> the matching end?
> 
> -a

Sorry. It's a bit much. That's why I was holding back. Here's the whole 
script.

require 'kirbybase'
Dir.chdir("E:/pagecounts")
#First, create the database table.
db = KirbyBase.new
# If table exists, delete it.
db.drop_table(:pageinfo) if db.table_exists?(:pageinfo)
pageinfo_tbl = db.create_table(:pageinfo,
                                :filename,      {:DataType=>:String, 
:Index=>1},
                                :lconstant,     :String,
                                :compcode,      :String,
                                :primecode,     :Integer,
                                :costcenter,    :String,
                                :acctgroup,     :Integer,
                                :blank,         :String,
                                :description,   :String,
                                :pagecount,     :Float,
                                :sjccode,       :String,
                                :fullname,      {:DataType=>:String, 
:Index=>2}
                   )
# Import the csv file.
pageinfo_tbl.import_csv('McArdle_indexes.csv')

=begin
Parse each postscript print file in the polled directory. Create 
variables for:
the number of pages in each file; the number of blank pages in each 
file; and,
what exact pages are blank.
=end
Dir.glob("*.ps").each do |psfile|
  file_contents = File.read(psfile)
  file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do
    totalpages = $1
    if (totalpages.to_i % 2) !=0 then
      newtotalpages = totalpages.to_i + 1
      file_contents << "\%\%Blank page for Asura.\n\%\%Page:
       #{newtotalpages.to_i}\nshowpage\n"
      File.open(psfile, "w") { |f| f.print file_contents }
      FileUtils.touch(psfile)
    end

=begin
Find blank pages in the postscript file. Look for the regular expression 
that
sees a page callout followed by postscript data that does not include
data in parentheses. Any type on a postscript page is enclosed in 
parentheses,
so, that's why this is a legitimate search. Blank pages have no 
parenthesized
data.
=end
  blanks = []
  file_contents.scan(/\%\%Page: [()0-9{1,5}] 
([0-9]{1,5})\n[^\(.*\)]\%\%Page/)
    do |match|
  blanks.push($1)
  end
  file_contents.scan(/\%\%Blank page for Asura.\n/) do |match|
  blanks.push(totalpages.to_i + 1)
  end

=begin
Open a "pageinfo" file. Put page information about the file into it.
Notice that the variable for the total number of pages differs depending 
on whether a "newtotalpages" variable exists. And, that variable only 
exists if the original page count was odd and a blank had to be added.
=end
  filename = File.basename("#{psfile}", '.ps')
  pageinfofile = File.basename("#{psfile}", '.ps') + ".pageinfo"
  File.open("E:/pagecounts/#{pageinfofile}", "a") do |fileinfo|
   if newtotalpages then
    fileinfo << #{filename}\n << "Total number of pages in this PDF:
      #{newtotalpages}\n" <<
	"Number of blank pages in this PDF: #{blanks.size}\n"	<<
	"Specific pages that are blank in this PDF: " <<
        "#{blanks.join(', ')}\n"
   else
    fileinfo << #{filename}\n <<
    "Total number of pages in this PDF: #{totalpages}\n" <<
    "Number of blank pages in this PDF: #{blanks.size}\n"	<<
    "Specific pages that are blank in this PDF: " <<
    "#{blanks.join(', ')}\n"
   end
   end
   end
end

=begin
Back to the database table. . . .
Query against the table and match the filename in the directory with 
whichever entry
in the "filename" column of the table matches. Then, if there's a match, 
populate
the "pagecount" field in that row of the table with the variable for the 
page count, as
found above. That variable name is "newtotalpages."
=end

Dir.glob("*.ps").each do |dirfile|
result = pageinfo_tbl.select(:filename) { |r| dirfile =~
  Regexp.new(r.filename) }
pageinfo_tbl.update { |r| r.name == 
{filename}.set(:pagecount=>#{newtotalpages}) } unless result.nil?
end





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