Rename Files

First off. Thanks for hanging with me on this. Second Just to be clear. I’ve done the following:

Copied the second ruby script you gave me into text edit:

#!/usr/bin/env ruby -w
if ARGV.size != 1
  puts "USAGE: Supply a folder of files to rename."
  exit
end

dir_path = ARGV[0]

Dir.entries(dir_path).each do |file_name|
  next if file_name =~ /^\\./
  begin
    file_path = "#{dir_path}#{file_name}"
    u_name = file_name.downcase.gsub(/\s/, '_')
    new_name = "#{dir_path}#{u_name}"
    File.rename(file_path, new_name)
  rescue => e
    puts "Error renaming: #{file_name}\nError: #{e}"
  end
end


–added your adjusted “DIR_TO_IGNORE = [‘.’, ‘…’, ‘.DS_Store’]” line

#!/usr/bin/env ruby -w
if ARGV.size != 1
  puts "USAGE: Supply a folder of files to rename."
  exit
end

dir_path = ARGV[0]
DIR_TO_IGNORE = ['.', '..', '.DS_Store']
Dir.entries(dir_path).each do |file_name|
  next if file_name =~ /^\\./
  begin
    file_path = "#{dir_path}#{file_name}"
    u_name = file_name.downcase.gsub(/\s/, '_')
    new_name = "#{dir_path}#{u_name}"
    File.rename(file_path, new_name)
  rescue => e
    puts "Error renaming: #{file_name}\nError: #{e}"
  end
end

–And Ran the following Applescript

set thePath to quoted form of (POSIX path of ((path to movies folder as Unicode text) & "myFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath


–I got the Following Return:

“Error renaming: .
Error: No such file or directory - /Users/comcastcable/Movies/myFolder. or /Users/comcastcable/Movies/myFolder.
Error renaming: …
Error: No such file or directory - /Users/comcastcable/Movies/myFolder… or /Users/comcastcable/Movies/myFolder…
Error renaming: .DS_Store
Error: No such file or directory - /Users/comcastcable/Movies/myFolder.DS_Store or /Users/comcastcable/Movies/myFolder.ds_store
Error renaming: My First Movie.mov
Error: No such file or directory - /Users/comcastcable/Movies/myFolderMy First Movie.mov or /Users/comcastcable/Movies/myFoldermy_first_movie.mov”

Ok, I think we have a winner.

it looks like there is missing a trailing slash in the path


set thePath to quoted form of (POSIX path of (path to movies folder) & "myFolder/")

SUCCESS!!! Thanks So Much for working through this.

The new Ruby worked. The additional slash was not needed. I ran the following with success:

#!/usr/bin/env ruby -w

if ARGV.size != 1
  puts "USAGE: Supply a folder of files to rename."
  exit
end

dir_path = ARGV[0]

Dir.entries(dir_path).each do |file_name|
  next if file_name =~ /^\./
  begin
    file_path = "#{dir_path}/#{file_name}"
    u_name = file_name.downcase.gsub(/\s/, '_')
    new_name = "#{dir_path}/#{u_name}"
    File.rename(file_path, new_name)
  rescue => e
    puts "Error renaming: #{file_name}\nError: #{e}"
  end
end

–saved as rename_files.rb on desktop

–then ran the following applescript:


set thePath to quoted form of (POSIX path of ((path to movies folder as Unicode text) & "myFolder"))
set rubyFile to quoted form of POSIX path of ((path to desktop as Unicode text) & "rename_files.rb")
do shell script "/usr/bin/ruby " & rubyFile & space & thePath

You=Awesome!

You are very welcome. Glad I could help.

Actually, Stefan was right but I had already added it to the Ruby script while he was replying. :smiley:

Ah, sneaky. Well then I thank Stefan as well.

This is a “rubyless” solution


set thePath to POSIX path of (path to movies folder) & "myFolder/"
set theFiles to list folder POSIX file thePath without invisibles
repeat with oneFile in theFiles
	set newFileName to do shell script "/bin/echo " & quoted form of oneFile & " | /usr/bin/tr A-Z' ' a-z_ "
	do shell script "/bin/mv " & quoted form of (thePath & oneFile) & space & quoted form of (thePath & newFileName)
end repeat

Wow stefan. You never cease to amaze me. That’s one less file to worry about. Thanks.

Give Stefan a little time and he can improve anyone’s code.

Nice job Sefan! :wink:

It wasn’t supposed as an improvement, just as a different approach

No, I didn’t mean it in a bad way. I meant it as a compliment. You really do make code better.
In this case it was a much better solution for the user and I appreciate that. :slight_smile: