Last active
January 22, 2017 03:32
-
-
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.
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
# 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