-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric_renamer.py
executable file
·94 lines (73 loc) · 2.25 KB
/
numeric_renamer.py
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
# Renames all the files in a folder by numbers
# Has no validations, tread carefully
############################################
# Imports
############################################
import glob
import os
import sys
############################################
# User Input section
############################################
# EXPECTED USER INPUT 1:
# Enter the path to the directory containing files to be renamed
# (Enter the trailing slash too)
search_dir = ''
# EXPECTED USER INPUT 2:
# Enter the file extension to process
# Doesn't support processing multiple file extension filters
# Example: extension = ".txt"
extension = ""
# OPTIONAL USER INPUT 3:
# Enter the mode of ordering files
# The modes are:
# Order by date
# Order by time
# The valid inputs are
# 'time' and 'name'
ordering_mode = 'time'
# OPTIONAL USER INPUT 4:
# Enter the starting number to be used to rename files
filename = 0
# OPTIONAL USER INPUT 5:
# Enter a flag to indicate if single digit numbers need to be prefixed with 0
zero_prefix_required = True
############################################
# Constants
############################################
ordering_mode_time = 'time'
ordering_mode_name = 'name'
debug_flag = "test"
############################################
# Process command line args (if any)
############################################
# Set Mode
if debug_flag in sys.argv:
debugMode = 1
else:
debugMode = 0
############################################
# Begin processing
############################################
# Read Start number
files = filter(os.path.isfile, glob.glob(search_dir + "*"))
if ordering_mode == ordering_mode_name:
files.sort(key=lambda x: os.path.basename(x))
else:
files.sort(key=lambda x: os.path.getmtime(x))
for f in files:
target_name = ""
# Identify target name
if zero_prefix_required:
if filename < 10:
target_name = search_dir + "0" + str(filename) + extension
else:
target_name = search_dir + str(filename) + extension
else:
target_name = search_dir + str(filename) + extension
# Final step - rename or print to console
if debugMode:
print "Renaming " + f + " to " + target_name
else:
os.rename(f, target_name)
filename += 1