Created
January 7, 2013 09:39
-
-
Save chenzx/4473721 to your computer and use it in GitHub Desktop.
Batch rename files
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
# Recursive scan a dir, & perform each-file rename operation: | |
def batch_files_from_dir(basepath) | |
sub_dirs = Array.new | |
Dir.new(basepath).entries.each { |subpath| | |
path = File.join(basepath, subpath) | |
if File.file?(path) then | |
if /0000[0-9]{3}$/.match(path) then | |
puts "#{path}" | |
new_path = path.gsub(/0000[0-9]{3}$/, '') | |
File.rename(path, new_path) | |
puts "\t--->#{new_path}" | |
end | |
elsif subpath!="." and subpath!=".." and File.directory?(path) then | |
batch_files_from_dir(path) | |
end | |
} | |
end | |
ARGV.each{ |path| | |
batch_files_from_dir( path ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment