forked from cpswan/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincdec.py
More file actions
100 lines (81 loc) · 3.31 KB
/
incdec.py
File metadata and controls
100 lines (81 loc) · 3.31 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Author: Chris Swan
Date: 5 Oct 2010
Updated with suggestions from Tim Swan: 6 Oct 2010
with thanks to Matt Weber (http://www.mattweber.org/2007/03/04/python-script-renamepy/)
Increment or decrement file names
"""
import os
import sys
from optparse import OptionParser
def ProcessFiles(options):
# Set the offset to increment or decrement
if options.inc:
offset=options.offset
else:
offset=0-options.offset
# Get directory to work on
if options.directory:
path = options.directory[0]
else:
path = os.getcwd()
# Create a list of files in the directory
fileList = os.listdir(path)
# Reverse the list so that we don't get file name collisions
if offset > 0:
fileList.reverse()
# Iterate across the list of files
for file in fileList:
# Get filename and the extension
name, ext = os.path.splitext(file)
oldname = os.path.join( path, name+ext )
# Replace - first step
if options.replace:
for vals in options.replace:
name = name.replace(vals[0], "*")
replace = vals[1]
# Extract digits from filename
ndigits = ''.join([letter for letter in name if letter.isdigit()])
# and the residual letters
nletters = ''.join([letter for letter in name if not letter.isdigit()])
# Process the inc/dec
if ndigits != '':
#Decrement the digits
newdigits = str(int(ndigits)+offset)
# and replace any zeros that may have been stripped by the int operation
zeropad=len(ndigits)-len(newdigits)
while zeropad>0:
newdigits = "0"+newdigits
zeropad=zeropad-1
else:
newdigits=""
# Replace - second step
if options.replace:
nletters = nletters.replace("*", replace)
# Create the new name
newname = os.path.join( path , nletters + newdigits + ext )
try:
# Check for verbose output
if options.verbose:
print(oldname + " -> " + newname)
# Rename the file
os.rename(oldname, newname)
except (OSError):
print >>sys.stderr, ("Error renaming "+file+" to "+newname)
if __name__ == "__main__":
"""
Parses command line and renames the files passed in
"""
# Create the options we want to parse
usage = "usage: %prog [options]"
optParser = OptionParser(usage=usage)
optParser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Use verbose output")
optParser.add_option("-i", "--inc", action="store_true", dest="inc", default=False, help="Increment")
optParser.add_option("-o", "--offset", type="int", dest="offset", default="1", help="Offset number")
optParser.add_option("-d", "--dir", action="append", type="string", nargs=1, dest="directory", help="Directory to work on if not PWD")
optParser.add_option("-r", "--replace", action="append", type="string", nargs=2, dest="replace", help="Replaces OLDVAL with NEWVAL in the filename", metavar="OLDVAL NEWVAL")
(options, args) = optParser.parse_args()
# Process files
ProcessFiles(options)
# exit successful
sys.exit(0)