Created
February 16, 2012 22:05
-
-
Save simonrad/1848212 to your computer and use it in GitHub Desktop.
Regex File-Renaming Utility
This file contains hidden or 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/python | |
| """ | |
| This is a command-line batch file-renaming utility. | |
| It uses regular expressions to transform filenames. | |
| This is especially helpful on Mac since Mac OS X does not include the Linux 'rename' command. | |
| Run with no arguments for help info. | |
| Author: Simon Radford (simonradf@gmail.com) | |
| """ | |
| import sys | |
| import os | |
| import re | |
| def main(dir, match_expr = r"([^.]*)", replace_expr = "(1).txt", dry_run = True, rename_hidden_files = False): | |
| print "match_expr =", match_expr | |
| print "replace_expr =", replace_expr | |
| for filename in os.listdir(dir): | |
| pathname = os.path.join(dir, filename) | |
| match = re.match(match_expr, filename) | |
| if match and match.end() == len(filename) and os.path.isfile(pathname) and (rename_hidden_files or not filename.startswith('.')): | |
| new_filename = replace_expr | |
| for n, group in enumerate(match.groups()): | |
| if group is not None: | |
| group_name = "(%s)" % (n + 1) | |
| new_filename = new_filename.replace(group_name, group) | |
| new_pathname = os.path.join(dir, new_filename) | |
| print "%s %s to %s" % ("Would rename" if dry_run else "Renaming", pathname, new_pathname) | |
| if not dry_run: | |
| os.rename(pathname, new_pathname) | |
| else: | |
| print "%s %s" % ("Would not rename" if dry_run else "Not renaming", pathname) | |
| if __name__ == "__main__": | |
| args = sys.argv[1:] | |
| dry_run = True | |
| rename_hidden_files = False | |
| while True: | |
| if args and args[-1] == "-f": | |
| dry_run = False | |
| del args[-1] | |
| elif args and args[-1] == "-h": | |
| rename_hidden_files = True | |
| del args[-1] | |
| else: | |
| break | |
| if not 1 <= len(args) <= 3: | |
| print "Usage: %s directory [match_expr] [replace_expr] [-f] [-h]" % sys.argv[0] | |
| print "Use -f option for a wet run." | |
| print "Use -h option to rename hidden files as well." | |
| print "For each file in directory, if the filename completely matches the match_expr, renames it to the replace_expr." | |
| print "The match_expr can be any regular expression recognized by Python's 're' module." | |
| print "The replace_expr is a string that can contain (1) to reference the first parenthesized regex group, (2) for the second, etc." | |
| print "The default will be to append '.txt' to all filenames that do not contain a dot." | |
| print "Make sure to enclose your regular expressions in quotes on the command line to prevent the shell from expanding them." | |
| sys.exit(1) | |
| if dry_run: | |
| print "Performing dry run. Use -f option for a wet run." | |
| else: | |
| print "Performing wet run." | |
| main(*args, dry_run = dry_run, rename_hidden_files = rename_hidden_files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment