Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created January 7, 2013 09:39
Show Gist options
  • Save chenzx/4473721 to your computer and use it in GitHub Desktop.
Save chenzx/4473721 to your computer and use it in GitHub Desktop.
Batch rename files
# 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