# How to re-name a file from Ruby script

**URL:** <https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313>\
**Category:** Ruby\
**Created:** [November 26, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313 "2006-11-26T21:12:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/1 "2006-11-26T21:12:07Z")

</div>

Hi all,

I have a folder containing many files with format  
LIPID5-2.001  
LIPID5-2.002  
…  
LIPID5-2.200

I want to change all of them into  
LIPID5-3.001  
LIPID5-3.002  
…  
LIPID5-3.200

I write a script as follows. Based on the screen output the filename is  
changed. But I go to the same folder and find the original file  
LIPID5-2.001 is still there and there is no file called LIPID5-3.001.  
Any comments?

Thanks,

Li

## 

path=‘C:\Ruby\self’

Dir.entries(path).each do |filename|

if filename=~/(LIPID5-2)(.\w+)/i  
p filename.gsub!(/LIPID5-2/,‘LIPID5-3’)  
end

end

##output

> ruby dir9.rb  
> “LIPID5-3.001”  
> Exit code: 0

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/2 "2006-11-26T21:23:27Z")

</div>

Quoting Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> LIPID5-3.002  
> Li  
> end  
> You have to commit the rename. gsub is only acting on the strings that  
> are the  
> filenames it does not actually commit a file name change. Try something  
> like  
> this:

File.rename(old\_name, new\_name)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/3 "2006-11-26T21:25:29Z")

</div>

Li Chen wrote:

> LIPID5-3.002  
> Li  
> end

I don’t see anything in the code that actually renames the file on the  
hard drive. You’ve computed the new name but have you executed something  
to actually change the name of the file?

By the way, unless you’ve literally only got a couple of hours to get  
this working, directories with lots of coded file names that have to get  
renamed to make sense to some other program is a very inefficient  
design, and for “extra credit” you might want to look at a more cogent  
re-design. Before there were industrial strength open source databases,  
a lot of code like that got written. But these days, it’s silly, even if  
you only have a few weeks, given how easy it is with frameworks like  
ActiveRecord / Rails to use a real database.

–  
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)  
[http://borasky-research.blogspot.com/](http://borasky-research.blogspot.com/)

If God had meant for carrots to be eaten cooked, He would have given  
rabbits fire.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/4 "2006-11-26T21:27:30Z")

</div>

Alle 21:12, domenica 26 novembre 2006, Li Chen ha scritto:

> LIPID5-3.002  
> Li  
> end
> 
> ##output
> 
> > ruby dir9.rb
> 
> “LIPID5-3.001”
> 
> > Exit code: 0

filename is only a ruby string, it has no relationship with your  
filesystem.  
If you want to change the name of a file, you should use the File.rename  
method:

Dir.entries(path).each do |filename|  
if filename=~/(LIPID5-2)(.\w+)/i  
File.rename(filename, filename.gsub(/LIPID5-2/,‘LIPID5-3’))  
end  
end

Stefano

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/5 "2006-11-26T21:27:30Z")

</div>

Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> LIPID5-3.002  
> Li
> 
> ## 
> 
> path=‘C:\Ruby\self’
> 
> Dir.entries(path).each do |filename|
> 
> if filename=~/(LIPID5-2)(.\w+)/i  
> p filename.gsub!(/LIPID5-2/,‘LIPID5-3’)  
> end

You’re changing a variable called filename but you’re not also talking  
to the filesystem and asking it to change the name of the file on disk.  
For that, you want something like File.rename.

m.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [November 26, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/how-to-re-name-a-file-from-ruby-script/77313/6 "2006-11-26T21:49:00Z")

</div>

> filename is only a ruby string, it has no relationship with your  
> filesystem.  
> If you want to change the name of a file, you should use the File.rename  
> method:
> 
> Dir.entries(path).each do |filename|  
> if filename=~/(LIPID5-2)(.\w+)/i  
> File.rename(filename, filename.gsub(/LIPID5-2/,‘LIPID5-3’))  
> end  
> end
> 
> Stefano

Stefano,

Thanks and method File.rename is what I want.

Li
