Last active
December 21, 2015 22:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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