0

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!

Community
  • 1
  • 1
vpoola88
  • 3,669
  • 4
  • 20
  • 21

2 Answers2

3

If you want to achieve replacing portion of the filename with something else, you should use a sub or gsub function of the String class:

filename = File.basename(f, File.extname(f))

Now in filename you have stored a String representing a current file's name. You can check it using instance_of? function of an Object class, just if you're curious:

filename.instance_of?(String)
# -> true

What you should do is to use gsub method to replace all occurencies of given string, or sub to replace only first of it. Here you can find detailed information of using these functions.

I suppose in your case this should do the trick:

filename.gsub('my name', 'my (first) name')
# 2nd question:
filename.gsub("my first name", "myfirstname")

Also, regular expressions are allowed in sub and gsub methods. You should give it a try if you want to write more complex patterns, for example strip all numbers from file.

  • for the 2nd question, your changing the whole string to be unspaced. But what would I do if I only wanted to unspace part of the string? I understand lstrip and rstrip deal with the end and beginning, but im not sure how to edit the middle. Here is an example: "My first name is Bob" to "My firstname is Bob". So I would think of doing the same for .gsub "first name", "firstname", but what if i wanted to change the string first to f first and then proceed? Likeso: "My first name is Bob" to "My fname is Bob" – vpoola88 Jul 17 '12 at 22:00
  • "My first name is Bob".gsub('first', 'f') => My f name is Bob. But I want to remove that space between f and name. – vpoola88 Jul 17 '12 at 22:35
  • `"My first name is Bob".gsub('first name', 'fname') "My fname is Bob"` or if the string is in `filename`, another alternate is `firstname['first name'] = 'fname'`. – the Tin Man Jul 17 '12 at 22:40
0

A nice way to create strings with variables in Ruby is:

first = "Eugene"
filename = "This is my #{first} name"

filename is equal to "This is my Eugene name"

so with the file portions you asked about:

"This is my #{folder_path}#{filename.gsub!(' ', '').capitalize}#{File.extname(f)}"

Removing spaces can be done with gsub (check out string class documentation http://www.ruby-doc.org/core-1.9.3/String.html):

filename.gsub(' ', '')

You can also use the File classes join method to concatenate strings into a path and avoid cross platform issues with slashes ('/' vs '\') For more see http://www.ruby-doc.org/core-1.9.3/File.html#method-c-join

Emery
  • 92
  • 1
  • 9