Skip to content

Instantly share code, notes, and snippets.

@igolden
Created March 5, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igolden/cd07c2b2d49f597e7937130b04cffa5d to your computer and use it in GitHub Desktop.
Save igolden/cd07c2b2d49f597e7937130b04cffa5d to your computer and use it in GitHub Desktop.
Traverse and rename files to slugs
##
# Traverse and rename files
#
# - needs cleanup
require 'shellwords'
path = "src/assets/images/catalog"
folders = Dir.entries(path)
def rename(name)
name.gsub("&", "").gsub(" ", " ").gsub(" ", "-").gsub("---", "-").downcase
end
folders.drop(2).each do |f|
unless f == '.ds_store' || f == '.DS_Store'
dirpath = "#{path}/#{f}"
sub_entries = Dir.entries(dirpath).drop(2)
sub_entries.each do |sub|
cmd = "mv #{dirpath}/#{sub.shellescape} #{dirpath}/#{rename(sub)}"
system(cmd)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment