|
| 1 | +#! python3 |
| 2 | + |
| 3 | +# renameDates.py - Renames filenames with American MM-DD-YYYY date format |
| 4 | +# to European DD-MM-YYYY |
| 5 | + |
| 6 | +import shutil, os, re |
| 7 | + |
| 8 | +# create a regex that matches files with American date format. |
| 9 | +datePattern = re.compile(r"""^(.*?) # all text before thte date |
| 10 | + ((0|1)?\d)- # one of two digits for the month |
| 11 | + ((0|1|2|3)\d)- # one or two digits for the day |
| 12 | + ((19|20)\d\d) # four digits for the year |
| 13 | + (.*?)$ # all text after the date |
| 14 | + """, re.VERBOSE) |
| 15 | + |
| 16 | +# Loop over the files in the working directory. |
| 17 | +for amerFilename in os.listdir('.'): |
| 18 | + mo = datePattern.search(amerFilename) |
| 19 | + |
| 20 | + # Skip files without a date. |
| 21 | + if mo == None: |
| 22 | + continue |
| 23 | + # Get the different parts of the filename. |
| 24 | + beforePart = mo.group(1) |
| 25 | + monthPart = mo.group(2) |
| 26 | + dayPart = mo.group(4) |
| 27 | + yearPart = mo.group(6) |
| 28 | + afterPart = mo.group(8) |
| 29 | + |
| 30 | + # Form the European-style filename. |
| 31 | + euroFilename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart |
| 32 | + # Get the full, absolute file paths. |
| 33 | + absWorkingDir = os.path.abspath('.') |
| 34 | + amerFilename = os.path.join(absWorkingDir, amerFilename) |
| 35 | + euroFilename = os.path.join(absWorkingDir, euroFilename) |
| 36 | + |
| 37 | + # Rename the files. |
| 38 | + print('Renaming "%s" to "%s"...' %(amerFilename,euroFilename)) |
| 39 | + shutil.move(amerFilename, euroFilename) |
| 40 | + |
| 41 | + |
0 commit comments