Skip to content

Instantly share code, notes, and snippets.

@tstone
Created December 8, 2009 18:51
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 tstone/251885 to your computer and use it in GitHub Desktop.
Save tstone/251885 to your computer and use it in GitHub Desktop.
import os
import sys
from dirsearch import DirSearch
# Global variables
PREFIX = ''
#
# Print the basic help to the screen
#
def print_help():
print
print 'Recursive Prefix Rename Command (Windows):'
print '-------------------------------'
print 'Automates the adding of a prefix to all filenames'
print
print
print 'Syntax:'
print '-------------------------------'
print 'rrename.py [dir] [prefix]'
print
print
#
# Called each time the DirSearch files a new file
#
def search_callback(item):
filename = os.path.basename(item)
if filename.find(PREFIX) != 0:
new_name = os.path.join(os.path.dirname(item), '%s%s' % (PREFIX, filename))
os.rename(item, new_name)
#
# Script entry point
#
if __name__ == '__main__':
# Make sure at least 2 arguments besides script name was given
if len(sys.argv) < 3 :
print_help()
else:
if '-help' or '/help' in sys.argv:
print_help()
else:
PREFIX = sys.argv[2]
d = DirSearch(sys.argv[1], show_output=True)
d.search(search_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment