Rename multiple files


#1

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.


#2

On Mon, May 20, 2013 at 10:53 AM, Eivinas Norusaitis
removed_email_address@domain.invalid 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.


#3

hi Jonas Jonaitis

Assume Filename is Ruby Forum

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

This would work.

RAJ


#4

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.


#5

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


#6

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


#7

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.


#8

On Mon, May 20, 2013 at 11:19 AM, Raj pal removed_email_address@domain.invalid 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.


#9

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.


#10

OK


#11

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