This is how you change file names with European date formatting, to American date formatting.
Coded and tested in Spyder.
# Europe dd/mm/yyyy # to # America mm/dd/yyyy import shutil, os, re # regex for European date datePattern = re.compile(r"""^(.*?) # pretext ((0|1|2|3)?\d)- # day ((0|1)?\d)- # month ((19|20)\d\d) # year (.*?)$ # post test """, re.VERBOSE) for europeanDate in os.listdir('.'): detectPattern = datePattern.search(europeanDate) # not detected if detectPattern == None: continue # detected preDate = detectPattern.group(1) day = detectPattern.group(2) month = detectPattern.group(4) year = detectPattern.group(6) postDate = detectPattern.group(8) # American date americanDate = preDate + month + '-' + day + '-' + year + postDate # Paths workingPath = os.path.abspath('.') americanDate = os.path.join(workingPath, americanDate) europeanDate = os.path.join(workingPath, europeanDate) # Rename files # shutil.move(europeanDate,americanDate) # for testing print('\nRenaming "%s" to "%s"...\n' % (europeanDate,americanDate))
Input
file-18-11-2017-datastamp.txt
file-22-7-2018-datestamp.txt
file-22-8-2018-datestamp.txt
file-26-10-2016-datestamp.txt
file-27-12-2018-datestamp.txt
Output
runfile(‘C:/Users/Homelab/.spyder-py3/_DATES/dates.py’, wdir=’C:/Users/Homelab/.spyder-py3/_DATES’)
Renaming “C:\Users\Homelab\.spyder-py3\_DATES\file-18-11-2017-datastamp.txt” to “C:\Users\Homelab\.spyder-py3\_DATES\file-11-18-2017-datastamp.txt”…
Renaming “C:\Users\Homelab\.spyder-py3\_DATES\file-22-7-2018-datestamp.txt” to “C:\Users\Homelab\.spyder-py3\_DATES\file-7-22-2018-datestamp.txt”…
Renaming “C:\Users\Homelab\.spyder-py3\_DATES\file-22-8-2018-datestamp.txt” to “C:\Users\Homelab\.spyder-py3\_DATES\file-8-22-2018-datestamp.txt”…
Renaming “C:\Users\Homelab\.spyder-py3\_DATES\file-26-10-2016-datestamp.txt” to “C:\Users\Homelab\.spyder-py3\_DATES\file-10-26-2016-datestamp.txt”…
Renaming “C:\Users\Homelab\.spyder-py3\_DATES\file-27-12-2018-datestamp.txt” to “C:\Users\Homelab\.spyder-py3\_DATES\file-12-27-2018-datestamp.txt”…