On Tue, May 11, 2010 at 4:27 PM, Jacob Davis <ismyhc / gmail.com> wrote:
> Jes=FAs Gabriel y Gal=E1n wrote:
>> On Tue, May 11, 2010 at 5:10 AM, Jacob Davis <ismyhc / gmail.com> 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] =3D 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 []=3D 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 =3D file_names.dup
>> new_name[build_number] =3D 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 =3D 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?

Yes, if you do

file_names =3D 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 =3D 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 =3D STDIN.gets.chomp.to_s

# Rename each file replacing build number with version number
file_names.each do |file_name|
  new_name =3D file_name.dup
  new_name[build_number] =3D version_number
  File.rename(file_name, new_name)
end

puts "done!"

something like that.

Jesus.