Hello,
Can someone help me diagnose what's going on with my script? Part of it 
is below. I need to open a file, read its contents, do something to its 
contents if a condition is true, then write new data to the end of the 
contents, of the open file. It's doing it. It does add what I want it to 
add, but, it adds it hundreds and hundreds of times, not just one time, 
which is all I want.
...
Dir.glob("*.ps").each do |$psfile|
 $filetime = File.ctime($psfile)
 $filetime = $filetime.to_s.gsub!(/ -.*$/, "")
 file_contents = File.read($psfile)
 file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do  #look for 
pagecount in file
  totalpages = $1                                     #put that count in 
variable
 end

 if (totalpages.to_i % 2) !=0 then                    #if odd, then add 
blank pg
  totalpages = totalpages.to_i + 1
  file_contents.gsub!(/.*$/, "\%\%Blank page for Asura.\n\%\%Page: 
#{totalpages.to_i}\nshowpage\n")
  end
 File.open("#{$psfile}", "a") { |f| f.print file_contents }
end
...

Thanks.

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