Skip to content

Instantly share code, notes, and snippets.

@blakerohde
Created July 31, 2013 06:13
Show Gist options
  • Save blakerohde/6119725 to your computer and use it in GitHub Desktop.
Save blakerohde/6119725 to your computer and use it in GitHub Desktop.
Used to remove '_conflict-YYYYMMDD-######' substrings from ownCloud-deemed conflicted files.
#!/usr/bin/env python
"""
owncloud-filename-fixer.py
Used to remove '_conflict-YYYYMMDD-######' substrings from ownCloud-deemed conflicted files.
:copyright: (c) 2013 by Blake Rohde.
:license: ISC, see LICENSE for more details.
"""
import fnmatch, os, argparse, sys
ex_conf_msg = '_conflict-20130729-185534'
ex_conf_msg_len = len(ex_conf_msg)
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('start_dir', default='',
help='the directory to being recursively searching and replacing the search string')
parser.add_argument('log_filename', default='',
help='the filename for the log of file renames to be logged')
parser.add_argument('--search_string', default='*_conflict-*',
help='the search string to remove/replace')
parser.add_argument('-s', '--simulate', action='store_true', default=False,
help='do not actually rename the files')
args = parser.parse_args(args=argv)
log = open(args.log_filename, 'a+')
for root, dirnames, filenames in os.walk(args.start_dir):
for filename in fnmatch.filter(filenames, args.search_string):
path = os.path.join(root, filename)
fname, ext = os.path.splitext(path)
if ext == '':
log.write('RENAME:[' + path + ']TO:[' + path[:-ex_conf_msg_len] + ']' + os.linesep)
if args.simulate == False:
os.rename(path, path[:-ex_conf_msg_len])
else:
log.write('RENAME:[' + path + ']TO:[' + fname[:-ex_conf_msg_len] + ext + ']' + os.linesep)
if args.simulate == False:
os.rename(path, fname[:-ex_conf_msg_len] + ext)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment