Skip to content

Instantly share code, notes, and snippets.

@przbadu
Last active January 22, 2017 03:32
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 przbadu/d6464d7639efc6e3d4ca670c3b412359 to your computer and use it in GitHub Desktop.
Save przbadu/d6464d7639efc6e3d4ca670c3b412359 to your computer and use it in GitHub Desktop.
Bulk rename files inside given directory path. Basically remove certain strings/text from the file name that we don't want in those file name and are common in all the files.
# Your directory path where all files are there which you want to rename
# without trailing / (slash)
dir_path = "your/dir/path/goes/here"
Dir.glob("#{dir_path}/*").each do |file|
# squeeze all whitespaces into single whitespaces
new_file_name = file.squeeze(' ')
base_name = File.basename(new_file_name)
if base_name.include?('Xamarin.Android Tutorial - ')
rename_to = new_file_name.gsub('Xamarin.Android Tutorial - ', '')
elsif base_name.include?('Xamarin Android Tutorial ')
rename_to = new_file_name.gsub('Xamarin Android Tutorial ', '')
elsif base_name.include?('Xamarin Android ')
rename_to = new_file_name.gsub('Xamarin Android ', '')
else
rename_to = new_file_name
end
File.rename(file, rename_to)
puts "Renamed from: #{file} \n to: #{rename_to}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment