Forum: Ruby Rename multiple files

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4b566fabbd098d71a3045a155b894ad2?d=identicon&s=25 Name S. (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.
"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.
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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
4b566fabbd098d71a3045a155b894ad2?d=identicon&s=25 Name S. (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.
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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
4b566fabbd098d71a3045a155b894ad2?d=identicon&s=25 Name S. (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
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (arc)
on 2013-05-20 12:09
OK
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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.
"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.
F1550c30952a41e1f08a14dedc55166e?d=identicon&s=25 Ronald R. (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.
F1550c30952a41e1f08a14dedc55166e?d=identicon&s=25 Ronald R. (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.
This topic is locked and can not be replied to.