Last active
January 20, 2016 01:25
-
-
Save ukazap/dd27571adc0bcebec64d to your computer and use it in GitHub Desktop.
Ruby script for recursively renaming current directory's files and folders to fix Windows invalid characters: /\?%*|"<>
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 | |
pwd = Dir.pwd | |
puts pwd + "\n#{"="*pwd.length}" | |
entries = Dir.glob(pwd+"/**/*", File::FNM_DOTMATCH) | |
renamed = 0 | |
entries.reverse.each do |file| | |
unless File.basename(file) =~ /^(\.\.|\.)\z/ | |
new_name = File.dirname(file)+"/"+File.basename(file).gsub(/[\/\\?%*:|"<>]/, "_") | |
if file != new_name | |
puts "#{file.gsub(pwd, "")} => #{new_name.gsub(pwd,"")}" | |
File.rename file, new_name | |
renamed += 1 | |
end | |
end | |
end | |
puts "\nRenamed #{renamed} of #{entries.size-2}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment