print " "
import os, re, string
setpath = raw_input("Path to the Directory Where Mac Directory & Filenames Need to be Made Sane: ")
print " "
print "--- Replace Bad Characters in Directory Names & Filenames ---"
print " "
def clean_names(setpath):
	bad = re.compile(r'%2f|%25|[*?<>/\|\\]') #search for these bad chars.
	for root, dirs, files in os.walk(setpath):
		for dir in dirs:
			badchars = bad.findall(dir)	# find all bad chars.
         		newdir = dir
         		for badchar in badchars:	# loop through each character in badchars  
         			print "replaced:	",badchar,"	in dir		",newdir,"		",
             			newdir = newdir.replace(badchar,'-') #replace bad chars.
				print newdir
			if newdir:	# If there are any bad characters in the name, do this:
             			newpath = os.path.join(root,newdir)
             			oldpath = os.path.join(root,dir)
             			os.rename(oldpath,newpath)
	for root, dirs, files in os.walk(setpath):
		for file in files:
			badchars = bad.findall(file)	# find all bad chars.
         		newfile = file
         		for badchar in badchars:	# loop through each character in badchars  
         			print "replaced:	",badchar,"	in file		",newfile,"		",
             			newfile = newfile.replace(badchar,'-') #replace bad chars.
				print newfile
			if newfile:	# If there are any bad characters in the name, do this:
             			newpath = os.path.join(root,newfile)
             			oldpath = os.path.join(root,file)
             			os.rename(oldpath,newpath)
clean_names(setpath)	#1	
clean_names(setpath)	#2
clean_names(setpath)	#3
clean_names(setpath)	#4
clean_names(setpath)	#5	Be recursive so program gets sub dirs too.
clean_names(setpath)	#6	You may add or remove as many of these as you need.
clean_names(setpath)	#7
clean_names(setpath)	#8
clean_names(setpath)	#9
clean_names(setpath)	#10
print " "
print "--- Done ---"
print " "
print "--- Remove Spaces from Beginning and Ending of Directory Names & Filenames ---"
print " "
def clean_spaces(setpath):
	for root, dirs, files in os.walk(setpath):
    		for dir in dirs:
        		old_dname = dir		#original name of dir as it exists in the filesystem.
        		new_dname = old_dname.strip()	#new name of dir with spaces striped from beginning and ending.
			if new_dname != old_dname:	#if space(s) are found...
				print "removed spaces from dir:",old_dname	#show user what's going on.
				newpath = os.path.join(root,new_dname)	#declare new path.
       				oldpath = os.path.join(root,old_dname)	#declare old path.
       				os.rename(oldpath,newpath)	#rename dir without spaces.
	for root, dirs, files in os.walk(setpath):
    		for file in files:
        		old_fname = file	#original name of file as it exists in the filesystem.
        		new_fname = old_fname.strip()	#new name of file with spaces striped from beginning and ending.
			if new_fname != old_fname:	#if space(s) are found...
				print "removed spaces from file:",old_fname	#show user what's going on.
				newpath = os.path.join(root,new_fname)	#declare new path.
       				oldpath = os.path.join(root,old_fname)	#declare old path.
       				os.rename(oldpath,newpath)	#rename file without spaces
clean_spaces(setpath)	#1
clean_spaces(setpath)	#2
clean_spaces(setpath)	#3
clean_spaces(setpath)	#4
clean_spaces(setpath)	#5	Be recursive as it doesn't hurt anything, although it's probably not needed for files.
clean_spaces(setpath)	#6
clean_spaces(setpath)	#7
clean_spaces(setpath)	#8
clean_spaces(setpath)	#9
clean_spaces(setpath)	#10
print " "
print "--- Done --- "
print " "
print "--- This program was written by Brad Tilley rtilley@vt.edu ---"
print " "