# What is it about File.rename?

**URL:** <https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116>\
**Category:** Ruby\
**Created:** [May 14, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116 "2007-05-14T14:39:31Z")\
**Posts on this page:** 5\
**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:** [May 14, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116/1 "2007-05-14T14:39:31Z")

</div>

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!

…

1. File.rename(pstxtfile, pstxtfile.to\_s.gsub(/(^.\*).pdf.pstxt/,  
“ps2k\_#{$1}.pstxt”))

…

The above works fine and renames all 172 files in the directory at  
present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs  
and gives me the following one entry remaining in my directory:

ps2k\_.pstxt

All the 172 files are gone! This doesn’t make sense to me. The  
“filetime” variables are to be used in something completely different  
and I don’t see why they’re affecting this simple renaming of files.

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:** [May 14, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116/2 "2007-05-14T16:04:33Z")

</div>

Peter B. 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!
> 
> …
> 
> 1. File.rename(pstxtfile, pstxtfile.to\_s.gsub(/(^.\*).pdf.pstxt/,  
> “ps2k\_#{$1}.pstxt”))
> 
> …
> 
> The above works fine and renames all 172 files in the directory at  
> present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs  
> and gives me the following one entry remaining in my directory:
> 
> ps2k\_.pstxt
> 
> All the 172 files are gone! This doesn’t make sense to me. The  
> “filetime” variables are to be used in something completely different  
> and I don’t see why they’re affecting this simple renaming of files.
> 
> Thanks,  
> Peter

Just a thought…

If I recall correctly, gsub! returns nil if no substitution was made.

Does changing…

$filetime = $filetime.to\_s.gsub!(/ -0500.\*$/, “”)

…to…

$filetime = $filetime.to\_s.gsub(/ -0500.\*$/, “”)

…have any effect?

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform

---

<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:** [May 14, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116/3 "2007-05-14T22:59:13Z")

</div>

On 5/14/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. #$totalpages = $totalpages.to\_s.chomp!
> 
> …
> 
> 1. 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/](http://talklikeaduck.denhaven2.com/)

---

<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:** [May 14, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116/4 "2007-05-14T23:02:13Z")

</div>

On 5/14/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try replacing line 20 with either  
> File.rename(pstxtfile, pstxtfile.to\_s.gsub(/(^.\*).pdf.pstxt/,  
> “ps2k\_\1.pstxt”))

Oops that should be “ps2k\_\1.pstxt” or ‘ps2k\_\1.pstxt’ The double  
quote string will interpret a single slash as an escape of the next  
character.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<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:** [May 14, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/what-is-it-about-file-rename/104116/5 "2007-05-14T16:38:09Z")

</div>

David M. wrote:

> Peter B. 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!
> > 
> > …
> > 
> > 1. File.rename(pstxtfile, pstxtfile.to\_s.gsub(/(^.\*).pdf.pstxt/,  
> > “ps2k\_#{$1}.pstxt”))
> > 
> > …
> > 
> > The above works fine and renames all 172 files in the directory at  
> > present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs  
> > and gives me the following one entry remaining in my directory:
> > 
> > ps2k\_.pstxt
> > 
> > All the 172 files are gone! This doesn’t make sense to me. The  
> > “filetime” variables are to be used in something completely different  
> > and I don’t see why they’re affecting this simple renaming of files.
> > 
> > Thanks,  
> > Peter
> 
> Just a thought…
> 
> If I recall correctly, gsub! returns nil if no substitution was made.
> 
> Does changing…
> 
> $filetime = $filetime.to\_s.gsub!(/ -0500.\*$/, “”)
> 
> …to…
> 
> $filetime = $filetime.to\_s.gsub(/ -0500.\*$/, “”)
> 
> …have any effect?
> 
> David
> 
> [http://rubyonwindows.blogspot.com](http://rubyonwindows.blogspot.com)

Thanks, David. Well, yeh, it does appear that the filetime stuff was  
messing me up. This is fairly old code, for me, meaning 3 or 4 months  
old, so, I modernized that filetime stuff a bit and now it seems to  
work! I basically just simplified it by changing:

$filetime = File.stat(pstxtfile).mtime

to

$filetime = File.mtime(pstxtfile)

Thanks again!  
-Peter
