Repository navigation
Expand file tree
/
Copy pathFileSorter.py
More file actions
102 lines (79 loc) · 4.13 KB
/
Copy pathFileSorter.py
File metadata and controls
102 lines (79 loc) · 4.13 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Made by 6augt!
import os
import shutil
from datetime import datetime
#---------------------------------------------------------------------------------------------------------
# FILE TYPE, DIRECTORY AND DESTINATION PATH CONFIGURATION
Available_file_types = ['.jpg', '.jpeg', '.png', '.gif', '.pdf', '.docx', '.txt', '.mp3', '.wav', '.mp4', '.avi', '.mkv', '.zip', '.rar', '.tar', '.gz', '.HEIC', '.HEIF', '.bmp', '.tiff', '.svg', '.webp', '.pptx', '.xlsx', '.csv', '.json', '.xml', '.html', '.css', '.js']
Common_directories = {
'downloads': os.path.join(os.path.expanduser('~'), 'Downloads'),
'desktop': os.path.join(os.path.expanduser('~'), 'Desktop'),
'documents': os.path.join(os.path.expanduser('~'), 'Documents'),
'pictures': os.path.join(os.path.expanduser('~'), 'Pictures'),
}
print(''.center(50, '-'))
print('''File location example or shortcuts (downloads, desktop, documents, pictures):
MAC: /Users/dario/Documents/FolderName
WINDOWS: C:\\Users\\dario\\Documents\\FolderName''')
Create = input('\nDo you want to create a new folder to move the files into? (y/n): ').lower().strip()
if Create in ['y', 'yes']:
New_folder = input('\nWhat do you want to name the new folder?: ').strip()
Parent_input = input('\nWhere do you want to create the new folder?: ').strip()
Parent_path = Common_directories.get(Parent_input.lower(), Parent_input)
To = os.path.join(Parent_path, New_folder)
os.makedirs(To, exist_ok=True)
elif Create in ['n', 'no']:
To_Input = input('\nWhere do you want to move the files?: ').strip()
To = Common_directories.get(To_Input.lower(), To_Input)
if not os.path.exists(To):
print(f'\nError: The destination path "{To}" does not exist. Please create the folder or choose an existing one.')
exit()
else:
print('\nError: Invalid input. Please enter "y" or "n".')
exit()
#---------------------------------------------------------------------------------------------------------
# FILE SORTING
From_Input = input('\nWhere are the files you want to move located?: ').strip()
From = Common_directories.get(From_Input.lower(), From_Input)
if not os.path.exists(From):
print(f'\nError: The source path "{From}" does not exist. Please provide a valid path.')
exit()
print(f"\nCommon file types: {', '.join(Available_file_types[:8])}...")
filetype = input('What types of files do you want to sort? (Separated by spaces): ').lower().split()
chosen_filetype = [ft if ft.startswith('.') else f'.{ft}' for ft in filetype]
if len(chosen_filetype) == 0:
print('\nError: No file types provided.')
exit()
target_extensions = tuple(chosen_filetype)
#---------------------------------------------------------------------------------------------------------
# MOVE TO DESTINATION
files_moved = 0
for file in os.listdir(From):
# Skip hidden system files (.DS_Store, etc.)
if file.startswith('.'):
continue
file_route = os.path.join(From, file)
if os.path.isdir(file_route):
continue
if file.lower().endswith(target_extensions):
file_info = os.stat(file_route)
size_mb = file_info.st_size / (1024 * 1024)
create_date = datetime.fromtimestamp(file_info.st_ctime).strftime('%Y-%m-%d')
mod_date = datetime.fromtimestamp(file_info.st_mtime).strftime('%Y-%m-%d')
if Create in ['y', 'yes']:
destiny_path = To
else:
type_file = os.path.splitext(file)[1][1:].upper()
destiny_path = os.path.join(To, type_file)
dest_file_path = os.path.join(destiny_path, file)
if os.path.exists(dest_file_path):
print(f"Skipped (already exists in destination): {file}")
continue
os.makedirs(destiny_path, exist_ok=True)
shutil.move(file_route, destiny_path)
files_moved += 1
print(f"Moved: {file} ({size_mb:.2f} MB | Created: {create_date} | Modified: {mod_date}) -> {destiny_path}")
if files_moved == 0:
print(f"\nNo {target_extensions} files were moved from {From}.")
else:
print(f"\nSuccessfully moved {files_moved} file(s) matching {target_extensions}.")