Skip to content

Commit 01e6465

Browse files
author
akashgiricse
committed
Created script and README.md
1 parent 0b2d024 commit 01e6465

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Renaming Files with American-Style Dates to European-Style Dates
2+
3+
Renames filenames with American MM-DD-YYYY date format to European DD-MM-YYYY
4+
5+
### Getting started
6+
Dependenciies:
7+
- Python 3.6.x
8+
- Ubuntu 17.04 or later or Linux Mint 18.x
9+
10+
#### 1. Clone this repoistory
11+
```bash
12+
git clone https://github.com/akashgiricse/ScriptsUsingPython.git
13+
```
14+
15+
### 2. Run this script in the directory in which you want to change the date format
16+
```bash
17+
python renameDates.py
18+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)