Skip to content

Instantly share code, notes, and snippets.

@MadBomber
Created October 28, 2012 03:07
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 MadBomber/3967288 to your computer and use it in GitHub Desktop.
Save MadBomber/3967288 to your computer and use it in GitHub Desktop.
Normalize file and directory names
#!/bin/env ruby
###########################################
###
## File: normalize_pathnames.rb
## Desc: renames directory and file names such that any
## embedded special characters are translated to an underscore.
#
$debug = ARGV.include?('-d') || ARGV.include?('--debug')
$changed = 0
require 'pathname'
def normalize_filename( pn, from_chars, to_chars )
return(pn) unless Pathname == pn.class
p = pn.dirname
f = pn.basename.to_s
if f.tr_s!(from_chars, to_chars)
$changed += 1
f = f[0,f.size-to_chars.size] while f.end_with?(to_chars)
f.gsub!(to_chars+'.', '.')
p2 = p + f
pn.rename(p2)
return(p2)
end
return(pn)
end # end of def normalize_filename
def normalize_directory( dir,
from_chars= "\-[]+ ',()",
to_chars = "_"
)
new_dir = normalize_filename( dir, from_chars, to_chars )
new_dir.children.each do | filename |
if filename.directory?
normalize_directory( filename, from_chars, to_chars )
else
normalize_filename( filename, from_chars, to_chars )
end
end # end of dir.children.each do | filename |
end # end of def normalize_directory
##################################
## main
normalize_directory( Pathname.pwd )
puts "Changed #{$changed} file and directory names."
__END__
# test script
rm -fr one_two
mkdir -p one\ two/three\ four
touch one\ two/three\ four/five\ six.txt
touch one\ two/three\ four/seven\ \-\ eight\ .txt
touch one\ two/three\ four/nine\ \-\ ten\(10\)\ .txt
normalize_pathnames.rb
ls -R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment