On 5/14/07, Peter Bailey <pbailey / bna.com> wrote:
> Hello,
> I'm going a bit nuts with a script of mine that doesn't seem to behave
> with file renaming. I've got 172 files in a directory, all with the
> extension ".pstxt."
>
> 1. Dir.glob("*.pstxt").each do |pstxtfile|
> 2.   $ps2kfile = File.basename(pstxtfile, ".pstxt")
> 3.   $filetime =  File.stat(pstxtfile).mtime
> 4.   #$filetime = $filetime.to_s.gsub!(/ -0500.*$/, "")
> 5.   #$totalpages = IO::readlines(pstxtfile).to_s
> 6.   #$totalpages = $totalpages.to_s.chomp!
>
> ...
>
> 20. File.rename(pstxtfile, pstxtfile.to_s.gsub(/(^.*)\.pdf\.pstxt/,
> "ps2k_#{$1}.pstxt"))

I think that your problem is here.  String#gsub with a string
replacement doesn't provide the use of $1 in the replacement string.
I'm not sure why it's working when it does.  Something before thoses
lines seems to be setting $1 to what you are expecting,  Line 4 is
going to reset $1 to nil since it doesn't capture anything.

Try replacing line 20 with either
 File.rename(pstxtfile, pstxtfile.to_s.gsub(/(^.*)\.pdf\.pstxt/,
"ps2k_\1.pstxt"))

or

File.rename(pstxtfile, pstxtfile.to_s.gsub(/(^.*)\.pdf\.pstxt/)
{"ps2k_#{$1}.pstxt")}

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/