-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRenamer.py
More file actions
66 lines (48 loc) · 1.86 KB
/
FileRenamer.py
File metadata and controls
66 lines (48 loc) · 1.86 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
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
import os
import shutil
import re
from tkinter import Tk, filedialog
def renameFile(filePath):
# Using regular expression to get date
match = re.match(
r"(\d{1,2}\s\w+(\s\w+)?\s\d{4})\s-\s(.+)\.(\w+)", filePath)
if match:
date_str, _, name, ext = match.groups()
# Name Jan to January if your format is like that.
months = {
'Jan': '01', 'Feb': '02',
'Mar': '03', 'Apr': '04',
'May': '05', 'Jun': '06',
'Jul': '07', 'Aug': '08',
'Sep': '09', 'Oct': '10',
'Nov': '11', 'Dec': '12'
}
for month_name, month_num in months.items():
date_str = date_str.replace(month_name, month_num)
formatted_date = '-'.join(date_str.split())
new_file_name = f"{formatted_date}-{name.replace(' ', '-').replace('.', '')}.{ext}"
return new_file_name
else:
return None
def renameFilesInFolder(inputFolder, outputFolder):
if not os.path.exists(outputFolder):
os.makedirs(outputFolder)
for filename in os.listdir(inputFolder):
filePath = os.path.join(inputFolder, filename)
if os.path.isfile(filePath):
newFilename = renameFile(filename)
if newFilename:
new_filePath = os.path.join(outputFolder, newFilename)
shutil.copy(filePath, new_filePath)
print(f"Renamed and copied: {filename} to {newFilename}")
def select_folder():
root = Tk()
root.withdraw()
folder_selected = filedialog.askdirectory(title="Select Folder")
return folder_selected
if __name__ == "__main__":
inputFolder = select_folder()
# Make an folder inside the selected folder
outputFolder = os.path.join(inputFolder, "Renamed Folder")
# Rename files and copy them to the output folder
renameFilesInFolder(inputFolder, outputFolder)