Skip to content

Instantly share code, notes, and snippets.

@ryanmasondavies
Last active December 21, 2015 22:59
Show Gist options
  • Save ryanmasondavies/6379077 to your computer and use it in GitHub Desktop.
Save ryanmasondavies/6379077 to your computer and use it in GitHub Desktop.
Ruby script to update file prefixes recursively. Useful to quickly change filenames to match a class prefix change in an Xcode project.
#!/usr/bin/env ruby
class String
def starts_with?(prefix)
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
end
old = 'RJD' # old prefix
new = 'CPT' # new prefix
Dir[File.join("/path/to/project/files/**/#{old}")].entries.each do |path|
basename = File.basename(path)
dirname = File.dirname(path)
if (basename.starts_with? old)
new_basename = new + basename[old.length, basename.length - old.length]
new_path = File.join(dirname, new_basename)
File.rename(path, new_path)
puts "#{basename} -> #{new_basename}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment