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.

···

--
Posted via http://www.ruby-forum.com/.

Take a look at to rename a file:

and to calculate the new name:

and/or

and

Hope this helps,

Jesus.

···

On Mon, May 20, 2013 at 10:53 AM, Eivinas Norusaitis <lists@ruby-forum.com> 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

hi Jonas Jonaitis

Filename='Ruby Forum'

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

This would work.

RAJ

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

Jesus.

···

On Mon, May 20, 2013 at 11:19 AM, Raj pal <lists@ruby-forum.com> wrote:

hi Jonas Jonaitis

Filename='Ruby Forum'

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

This would work.