Skip to content

Instantly share code, notes, and snippets.

@ukazap
Last active January 20, 2016 01:25
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 ukazap/dd27571adc0bcebec64d to your computer and use it in GitHub Desktop.
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: /\?%*|"<>
#!/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