From: "Jesús Gabriel y Galán" Date: 2010-05-11T23:42:08+09:00 Subject: Re: Problem renaming files On Tue, May 11, 2010 at 4:27 PM, Jacob Davis wrote: > Jesús Gabriel y Galán wrote: >> On Tue, May 11, 2010 at 5:10 AM, Jacob Davis 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? 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.