Created
November 7, 2017 15:52
-
-
Save raprasad/bc1374d670ee6ca94640bffcd544c5d7 to your computer and use it in GitHub Desktop.
rename files
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
""" | |
Recursively check directories to rename files. | |
Files ending with "_db.xml" are renamed "dublin_core.xml" | |
(1) Shows files that will be renamed. DOESN'T actually rename files. | |
>python rename_files.py [base_directory] | |
(2) Include "rename" to actually rename the files. | |
>python rename_files.py [base_directory] rename | |
""" | |
from __future__ import print_function | |
import os | |
from os.path import dirname, isdir, join, realpath, relpath | |
import sys | |
def msg(m): print(m) | |
def dashes(border='-'): msg(border * 40) | |
def msgt(m, border='-'): dashes(border); msg(m); dashes(border) | |
def rename_files(base_directory, really_rename=False): | |
"""Recursively check directories and rename files""" | |
if not isdir(base_directory): | |
msgt('Directory not found: %s' % base_directory) | |
return | |
rename_cnt = 0 | |
dir_cnt = 0 | |
# get the full path of the directory | |
# | |
base_directory = realpath(base_directory) | |
# "walk" through all the files and subdirectories | |
# | |
for subdir, dirs, files in os.walk(base_directory): | |
dir_cnt += 1 | |
msgt('Checking directory (%d): %s' % (dir_cnt, subdir), '=') | |
# Iterate through the file names | |
# | |
for filename in files: | |
# Does the filename match the pattern? | |
# | |
if filename.lower().endswith('_db.xml'): | |
rename_cnt += 1 | |
# yes! rename the file | |
# - original and new filepaths | |
orig_filepath = join(subdir, filename) | |
new_filepath = join(subdir, 'dublin_core.xml') | |
msgt('file found (%d) %s' % (rename_cnt, filename)) | |
msg('orig: %s' % orig_filepath) | |
msg('\nnew: %s\n' % new_filepath) | |
if really_rename: | |
os.rename(orig_filepath, new_filepath) | |
msg('> file renamed!') | |
if really_rename: | |
msgt('renaming results', '=') | |
msg('Directories checked: %d' % dir_cnt) | |
msg('Files renamed: %s' % rename_cnt) | |
else: | |
msgt('--Test run--', '=') | |
msg('Directories checked: %d' % dir_cnt) | |
msg('# Files that would be renamed: %s' % rename_cnt) | |
dashes() | |
def show_instructions(): | |
msgt('Usage') | |
msg('(1) Shows files that will be renamed. DOESN\'T actually rename files.') | |
msg(' >python rename_files.py [base_directory]') | |
msg('\n(2) Include "rename" to actually rename the files.') | |
msg(' >python rename_files.py [base_directory] rename\n\n') | |
if __name__ == '__main__': | |
num_args = len(sys.argv) | |
if num_args == 2: | |
rename_files(sys.argv[1]) | |
elif num_args == 3: | |
if sys.argv[2] == 'rename': | |
rename_files(sys.argv[1], True) | |
else: | |
show_instructions() | |
else: | |
show_instructions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment