A python script that renames (moves) files to a destination directory. However it will not overwrite existing files. The script uses a renaming strategy where a count is incremented in order to avoid naming conflicts.
Example usage::
mv-rename a.ext b.ext target-dir/
would mv a.ext and b.ext into the target directory. If::
target-dir/a.ext
target-dir/b.ext
already exist, the newly moved files would be named::
target-dir/a-<N>.ext
target-dir/b-<M>.ext
where <N> and <M> are the lowest numbers such that there is no conflict.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #!/usr/bin/env python
#
# Copyright John Reid 2012
#
"""
A python script that renames (moves) files to a destination
directory. However it will not overwrite existing files. The
script uses a renaming strategy where a count is incremented
in order to avoid naming conflicts.
Example usage::
mv-rename a.ext b.ext target-dir/
would mv a.ext and b.ext into the target directory. If::
target-dir/a.ext
target-dir/b.ext
already exist, the newly moved files would be named::
target-dir/a-<N>.ext
target-dir/b-<M>.ext
where <N> and <M> are the lowest numbers such that there
is no conflict.
"""
import sys, os
def show_options(exitval):
"Show the options and exit."
print 'USAGE: %s files... [destination directory]' % sys.argv[0]
sys.exit(exitval)
#
# Parse arguments
#
if len(sys.argv) < 3:
show_options(-1)
dst_dir = sys.argv[-1]
files = sys.argv[1:-1]
if not os.path.isdir(dst_dir):
print '%s is not a directory.' % dst_dir
show_options(-2)
for file in files:
if not os.path.exists(file):
print '%s does not exist.' % file
show_options(-3)
#
# Move files
#
for file in files:
basename = os.path.basename(file)
head, tail = os.path.splitext(basename)
dst_file = os.path.join(dst_dir, basename)
# rename if necessary
count = 0
while os.path.exists(dst_file):
count += 1
dst_file = os.path.join(dst_dir, '%s-%d%s' % (head, count, tail))
#print 'Renaming %s to %s' % (file, dst_file)
print dst_file
os.rename(file, dst_file)
|
I'm going to need to modify this to have the versionized file name like this <N>-a.ext instead