-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
179 lines (152 loc) · 6.44 KB
/
Copy pathmain.py
File metadata and controls
179 lines (152 loc) · 6.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import re
import shutil
from datetime import date, datetime
from colorama import Fore, Style, init
init()
# Regular expressions to detect dates in filenames
date_pattern = re.compile(r'\d{8}')
date_pattern2 = re.compile(r'\d{4}-\d{2}-\d{2}')
date_pattern3 = re.compile(r'\d{8}-\d{4}-\d{2}-\d{2}')
def creategrouptxtfile(grouptxtfile):
with open('file_group.txt', 'w') as f:
for item in grouptxtfile:
f.write(item+"/n")
def find_date_pattern(file,grouptxtfile):
"""Find out which of three date patterns the file_uses"""
date_patterns = [
r'\d{8}',
r'\d{4}-\d{2}-\d{2}',
r'\d{8}-\d{4}-\d{2}-\d{2}'
]
for pattern in date_patterns:
match3 = re.search(pattern, file)
if match3:
pattern_literal = pattern.replace("\\", r"\\")
new_file_name = re.sub(pattern, pattern_literal, file)
grouptxtfile.append(new_file_name)
return pattern
return None
def is_ambiguous(date_str):
"""Check if a date string can be interpreted in multiple valid ways."""
# Try interpreting as YYYYMMDD
try:
date1 = datetime.strptime(date_str, "%Y%m%d").date()
except ValueError:
date1 = None
# Try interpreting as YYYYDDMM
try:
date2 = datetime.strptime(date_str, "%Y%d%m").date()
except ValueError:
date2 = None
# Ambiguous if both interpretations are valid and result in different dates
return date1 and date2 and date1 != date2
def subtracter(execution_date, file_date):
"""Calculate the difference in days between two dates."""
return (execution_date - file_date).days
def extract_date_from_filename(filename):
"""Extract date object from filename if it contains a valid date format."""
match = date_pattern.search(filename) or date_pattern2.search(filename) or date_pattern3.search(filename)
if match:
date_str = match.group()
try:
if '-' in date_str and len(date_str) == 17:
compact, hyphenated = date_str.split('-')
date1 = datetime.strptime(compact, "%Y%m%d").date()
date2 = datetime.strptime(hyphenated, "%Y-%m-%d").date()
return date1, date2
elif '-' in date_str:
return datetime.strptime(date_str, "%Y-%m-%d").date()
else:
if is_ambiguous(date_str):
return "ambiguous"
return datetime.strptime(date_str, "%Y%m%d").date()
except ValueError:
pass
else:
pass
return None
def sort_files_by_date_diff(date_diff, filename, invalid_files, valid_files, ambiguous_files, invalid_file_path, ambiguous_file_path):
"""Classify files into valid, invalid or ambiguous based on date difference."""
if date_diff == "ambiguous":
ambiguous_files.append(filename)
try:
shutil.move(os.path.join(file_directory, filename), ambiguous_file_path)
except shutil.Error:
pass
elif date_diff > 5:
invalid_files.append(filename)
try:
shutil.move(os.path.join(file_directory, filename), invalid_file_path)
except shutil.Error:
pass
else:
valid_files.append(filename)
return valid_files, invalid_files, ambiguous_files
def display_results(valid_files, invalid_files, ambiguous_files):
"""Print categorized results to console."""
print(Fore.GREEN + "FILES WHICH ARE VALID:" + Style.RESET_ALL)
for file in valid_files:
print("✅", file)
find_date_pattern(file,grouptxtfile)
match2 = re.search(r"(\d{4})[_-]?(\d{2})",file)
if match2:
year = match2.group(1)
month = match2.group(2)
try:
os.makedirs("autoarchive/" + year + "/" + month)
shutil.move( "test_files/"+file, "autoarchive/" + year + "/" + month)
except FileExistsError:
shutil.move( "test_files/"+file, "autoarchive/" + year + "/" + month)
print("\n" + Fore.YELLOW + "FILES MOVED TO AMBIGUOUS FOLDER:" + Style.RESET_ALL)
for file in ambiguous_files:
print("❓", file)
print("\n" + Fore.RED + "FILES MOVED TO INVALID FOLDER:" + Style.RESET_ALL)
for file in invalid_files:
print("❌", file)
print("\nSummary:")
print("Valid files:", len(valid_files))
print("Ambiguous files:", len(ambiguous_files))
print("Invalid files:", len(invalid_files))
if __name__ == "__main__":
# Set paths and initialize lists
file_directory = "test_files"
invalid_file_path = 'invalid'
ambiguous_file_path = 'ambiguous'
current_date = date.today()
# Create directories if they don't exist
os.makedirs(invalid_file_path, exist_ok=True)
os.makedirs(ambiguous_file_path, exist_ok=True)
valid_files = []
invalid_files = []
ambiguous_files = []
grouptxtfile = []
# Process each file
try:
files = os.listdir(file_directory)
for file in files:
file_date = extract_date_from_filename(file)
if file_date:
if file_date == "ambiguous":
sort_files_by_date_diff("ambiguous", file, invalid_files, valid_files, ambiguous_files, invalid_file_path, ambiguous_file_path)
elif isinstance(file_date, tuple):
# Handle the case where two dates were found
date_diff1 = subtracter(current_date, file_date[0])
date_diff2 = subtracter(current_date, file_date[1])
# Use the smaller difference
date_diff = min(date_diff1, date_diff2)
sort_files_by_date_diff(date_diff, file, invalid_files, valid_files, ambiguous_files, invalid_file_path, ambiguous_file_path)
else:
date_diff = subtracter(current_date, file_date)
sort_files_by_date_diff(date_diff, file, invalid_files, valid_files, ambiguous_files, invalid_file_path, ambiguous_file_path)
else:
invalid_files.append(file)
try:
shutil.move(os.path.join(file_directory, file), invalid_file_path)
except shutil.Error:
pass
except FileNotFoundError:
print(f"Directory not found: {file_directory}")
# Display final results
display_results(valid_files, invalid_files, ambiguous_files)
creategrouptxtfile(grouptxtfile)