Rename multiple files

Hi,

Could anyone help me in writing a code which could replace file names
letters to lowercase and spaces to dashes? For example:

Ruby Forum to ruby-forum

Thanks.

On Mon, May 20, 2013 at 10:53 AM, Eivinas Norusaitis
[email protected] wrote:

Hi,

Could anyone help me in writing a code which could replace file names
letters to lowercase and spaces to dashes? For example:

Ruby Forum to ruby-forum

Take a look at to rename a file:

http://ruby-doc.org/core-2.0/File.html#method-c-rename

and to calculate the new name:

http://ruby-doc.org/core-2.0/String.html#method-i-tr
and/or
http://ruby-doc.org/core-2.0/String.html#method-i-gsub

and
http://ruby-doc.org/core-2.0/String.html#method-i-downcase

Hope this helps,

Jesus.

hi Jonas Jonaitis

Assume Filename is Ruby Forum

File.rename(Filename, Filename.downcase!.gsub!(32.chr,’-’))

This would work.

RAJ

path = “D:\Images”
Dir.open(path).each do
File.rename(Filename, Filename.downcase!.gsub!(32.chr,’-’))
end

What I would also really want is to set a path to a folder with multiple
files and enter a code, so it would make the changes to all file names.

My code is missing something.

i don’t understand exactly what are you asking here,

Do you want to change the name of all the files(downcase and - included)
under a particular folder?

RAJ

It’s sorted now, thanks.

path = “D:\Images”
Dir.open(path).each do |p|
next if p.match(/^./)
old = path + “\” + p
new = path + “\” + p.downcase.gsub(’ ', ‘-’)
File.rename(old, new)
puts old + " => " + new
end

You might find it more efficient to check that the file actually needs
renaming as well. With your current system you’re carrying on
regardless.

On Mon, May 20, 2013 at 11:19 AM, Raj pal [email protected] wrote:

hi Jonas Jonaitis

Filename=‘Ruby Forum’

File.rename(Filename, Filename.downcase!.gsub!(32.chr,’-’))

This would work.

This might not work because the bang versions return nil if no change
was made.

Jesus.

Name S. wrote in post #1109581:

Hi,

Could anyone help me in writing a code which could replace file names
letters to lowercase and spaces to dashes? For example:

Ruby Forum to ruby-forum

Thanks.

Are you looking how to batch rename files easily. Try “KrojamSoft
BatchRename program”…It is a great tool.

OK

Are you looking how to batch rename files easily. Try “KrojamSoft
BatchRename program”…It is a great tool.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs