Forum: Ruby Rename multiple files

3334f4a1116fd40a362c63a5aa4eca3a?d=identicon&s=25 Name Surname (neivinas)
on 2013-05-20 10:53
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-05-20 11:06
(Received via mailing list)
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
>

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.
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-05-20 11:19
hi Jonas Jonaitis

Assume Filename is Ruby Forum

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

This would work.

RAJ
3334f4a1116fd40a362c63a5aa4eca3a?d=identicon&s=25 Name Surname (neivinas)
on 2013-05-20 11:45
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.
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-05-20 12:05
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
3334f4a1116fd40a362c63a5aa4eca3a?d=identicon&s=25 Name Surname (neivinas)
on 2013-05-20 12:07
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
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-05-20 12:09
OK
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-05-20 12:14
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-05-20 12:14
(Received via mailing list)
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.

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

Jesus.
6c0842f397c7b7339a61bffd4464e8d1?d=identicon&s=25 Ronald Ron (ronald23)
on 2017-04-20 11:20
Name Surname 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.
6c0842f397c7b7339a61bffd4464e8d1?d=identicon&s=25 Ronald Ron (ronald23)
on 2017-04-20 11:20
Are you looking how to batch rename files easily. Try "KrojamSoft
BatchRename program"..It is a great tool.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.