I'm trying to write my first Ruby script that will rename files in a specific folder. I am basing my script off of this response : How to rename a file in Ruby?. However, I need help elaborating on some things. Here is the code from the above link that I currently have written out.
puts "Renaming files..."
folder_path = "/Desktop/untitled/"
Dir.glob( folder_path + "*" ).sort.each do |f|
filename = File.basename(f, File.extname(f))
File.rename( f, folder_path + filename.capitalize + File.extname(f))
end
puts "Renaming complete."
With this example, I understand that the script is simply capitalizing the name of the original file. But what do I do if I want to insert a segment in the name of the file. Say for example I have: "This is my name."
written out. What would I do if I just want to focus on the "my name" portion, and change it into something that would state:
"This is my (first) name."
Also, what if I wanted to remove a space:
"This is myfirstname."
Thanks so much!