Skip to content

Instantly share code, notes, and snippets.

@ryanmasondavies
Created January 13, 2016 16:27
Show Gist options
  • Save ryanmasondavies/dc40df434c12437ec567 to your computer and use it in GitHub Desktop.
Save ryanmasondavies/dc40df434c12437ec567 to your computer and use it in GitHub Desktop.
Rename mp3 files for burning to disk (for playing in car stereo)
path = ‘/path/to/music/folder’
puts "Renaming files..."
Dir.foreach(path) do |item|
next if item == '.' or item == '..'
new_name = File.basename(item, ".*")
# car works better with a-z 0-9 A-Z only
new_name.gsub!(/[^0-9A-Za-z ]/, '')
# specifies max length 20 chars
# doesn't specify if that includes extension, so cut that too
new_name = new_name[0, 20-4]
new_name.strip!
# re-add extension
new_name = new_name + '.mp3'
# rename file to use new name
File.rename(path + '/' + item, path + '/' + new_name)
puts new_name
end
puts "Renaming complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment