-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameDates.py
More file actions
42 lines (32 loc) · 1.24 KB
/
renameDates.py
File metadata and controls
42 lines (32 loc) · 1.24 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
#! python3
# renameDates.py - Renames filenames with American MM-DD-YYYY date format
# to European DD-MM-YYYY format
import shutil, os, re
#Regex that matches American date format MM-DD-YYYY
datePatter = re.compile("""^(.*?) # all text before the date
((0|1)?\d)- # one or two digits for the month
((0|1|2|3)?\d)- # one or two digits for the day
((19|20)\d\d) # four digits for the year
(.*?)$ # all text after the date
""", re.VERBOSE)
#Loop over the files in the working directory.
for amerFilename in os.listdir('.'):
mo = datePattern.seach(amerFilename)
#skip files without a date
if mo == None:
continue
# Get different parts of the filename
beforePart = mo.group(1)
monthPart = mo.group(2)
dayPart = mo.group(4)
yearPart = mo.group(6)
afterPart = mo.group(8)
# Form the European-style filename
euroFilename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart
# get the full, absolute filepaths
absWorkingDir = os.path.abspath('.')
amerFilename = os.path.join(absWorkingDir, amerFilename)
euroFilename = os.path.join(absWorkingDir, euroFilename)
# rename the files
print('Renaming "%s" to "%s"...' % (amerFilenames, euroFilename))
#shutil.move(amerFilename, euroFilename) # UNCOMMENT AFTER TESTING