# Problem renaming files

**URL:** <https://www.ruby-forum.com/t/problem-renaming-files/188385>\
**Category:** Ruby\
**Created:** [May 11, 2010, 5:10am UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385 "2010-05-11T05:10:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob480](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jacob480](https://www.ruby-forum.com/u/jacob480)\
**Post date:** [May 11, 2010, 5:10am UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385/1 "2010-05-11T05:10:20Z")

</div>

Hi Everyone,

Im new to ruby and am having a hard time with something Im working on.

I want to write a script that takes file names as arguments, then uses  
gets to get two values, and then renames the files based on those  
values.

example:

I want to rename mulitple files, removing the build number, and  
replacing it with another number.

engine.105 -\> rename to engine.8

Anyways, so right now Im just working on doing this with one file at a  
time. I realize this could be done without a script, but I want to end  
up doing this with multiple files at once. My script is probably not the  
most efficient way either, but I’d just like to know what Im doing wrong  
with how Im doing it. Just so I can learn…

Below is the code… I run the file as:

ruby c.rb engine.105

I then enter the build number which is 105  
Next I enter the version number I want to replace the build number with  
which is 5

The following is what I get:

c.rb:14:in `rename’: No such file or directory - engine.5 or 5  
(Errno::ENOENT)  
from c.rb:14

=====================

#!/usr/bin/env ruby

# Filename arguments

file\_names = ARGV.dup.to\_s

# Have user input build number to be removed

puts “Please enter the build number you wish to be removed from the  
file”  
build\_number = STDIN.gets.chomp.to\_s

# Have user input version number to be appended to file name

puts “Please enter the version number you wish to be appended”  
version\_number = STDIN.gets.chomp.to\_s

# Rename file replacing build number with version number

File.rename(file\_names, file\_names[build\_number]= version\_number)

puts “done!”

---

<div class="post-metadata">

**Author:** ![jacob480](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jacob480](https://www.ruby-forum.com/u/jacob480)\
**Post date:** [May 11, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385/2 "2010-05-11T16:27:09Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Tue, May 11, 2010 at 5:10 AM, Jacob D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to rename mulitple files, removing the build number, and  
> > Below is the code… I run the file as:  
> > (Errno::ENOENT)
> > 
> > # Have user input build number to be removed
> > 
> > puts “done!”
> 
> There are two problems here. The first one is that  
> file\_names[build\_number] = version will return the right hand side  
> expression, not the new value of the string, so you will be passing a  
> “5” to the rename method, not “engine.5”. The second is that by  
> calling []= on file\_names you are changing the object referenced by  
> file\_names, not making a new one, so you are passing “engine.5” as the  
> first argument to rename. Try this instead:
> 
> # Rename file replacing build number with version number
> 
> new\_name = file\_names.dup  
> new\_name[build\_number] = version\_number  
> File.rename(file\_names, new\_name)
> 
> Also, when you change things to receive multiple file names as  
> arguments to your script, you should probably change this line:
> 
> file\_names = ARGV.dup.to\_s
> 
> because that will join all the file names into a single string, which  
> is not what you want, I think.
> 
> Jesus.

Awesome! Yeap that worked! I see where I was making the mistakes last  
night. I guess its good to step away from it for a bit everyonce in a  
while.

So, I should remove the .to\_s so that it will populate the multiple  
names into an array correct?

Thanks!!!  
Jacob

---

<div class="post-metadata">

**Author:** ![jacob480](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jacob480](https://www.ruby-forum.com/u/jacob480)\
**Post date:** [May 11, 2010, 8:42am UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385/3 "2010-05-11T08:42:45Z")

</div>

On Tue, May 11, 2010 at 5:10 AM, Jacob D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to rename mulitple files, removing the build number, and  
> Below is the code… I run the file as:  
> (Errno::ENOENT)
> 
> # Have user input build number to be removed
> 
> puts “done!”

There are two problems here. The first one is that  
file\_names[build\_number] = version will return the right hand side  
expression, not the new value of the string, so you will be passing a  
“5” to the rename method, not “engine.5”. The second is that by  
calling []= on file\_names you are changing the object referenced by  
file\_names, not making a new one, so you are passing “engine.5” as the  
first argument to rename. Try this instead:

# Rename file replacing build number with version number

new\_name = file\_names.dup  
new\_name[build\_number] = version\_number  
File.rename(file\_names, new\_name)

Also, when you change things to receive multiple file names as  
arguments to your script, you should probably change this line:

file\_names = ARGV.dup.to\_s

because that will join all the file names into a single string, which  
is not what you want, I think.

Jesus.

---

<div class="post-metadata">

**Author:** ![jacob480](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jacob480](https://www.ruby-forum.com/u/jacob480)\
**Post date:** [May 11, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385/4 "2010-05-11T16:42:48Z")

</div>

On Tue, May 11, 2010 at 4:27 PM, Jacob D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > file\_names[build\_number] = version will return the right hand side  
> > File.rename(file\_names, new\_name)
> 
> Awesome! Yeap that worked! I see where I was making the mistakes last  
> night. I guess its good to step away from it for a bit everyonce in a  
> while.
> 
> So, I should remove the .to\_s so that it will populate the multiple  
> names into an array correct?

Yes, if you do

file\_names = ARGV.dup

you get an array of file names. Then you can iterate through that  
array doing the operation to each of them:

# Have user input build number to be removed

puts “Please enter the build number you wish to be removed from the  
file”  
build\_number = STDIN.gets.chomp.to\_s

# Have user input version number to be appended to file name

puts “Please enter the version number you wish to be appended”  
version\_number = STDIN.gets.chomp.to\_s

# Rename each file replacing build number with version number

file\_names.each do |file\_name|  
new\_name = file\_name.dup  
new\_name[build\_number] = version\_number  
File.rename(file\_name, new\_name)  
end

puts “done!”

something like that.

Jesus.

---

<div class="post-metadata">

**Author:** ![jacob480](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jacob480](https://www.ruby-forum.com/u/jacob480)\
**Post date:** [May 11, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/problem-renaming-files/188385/5 "2010-05-11T17:04:27Z")

</div>

2010/5/11 Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Tue, May 11, 2010 at 4:27 PM, Jacob D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Jesús Gabriel y Galán wrote:
> > 
> > > On Tue, May 11, 2010 at 5:10 AM, Jacob D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So, I should remove the .to\_s so that it will populate the multiple  
> > names into an array correct?
> 
> Yes, if you do
> 
> file\_names = ARGV.dup
> 
> you get an array of file names. Then you can iterate through that  
> array doing the operation to each of them:

You do not even need to #dup because ARGV _is_ an Array already.

$ ruby19 -e ‘p ARGV.class’  
Array

> # Have user input build number to be removed
> 
> puts “Please enter the build number you wish to be removed from the  
> file”  
> build\_number = STDIN.gets.chomp.to\_s
> 
> # Have user input version number to be appended to file name
> 
> puts “Please enter the version number you wish to be appended”  
> version\_number = STDIN.gets.chomp.to\_s

#to\_s is not needed but checking the input is probably also a good  
idea. You could simply do

version\_number = STDIN.gets.chomp  
Integer(version\_number) # will throw if not numeric

Kind regards

robert
