File size: 1,586 Bytes
8eaa451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re

DATA_DIRS = [
    r"backend\data\thông tin thuốc nội bộ",
    r"backend\data\cảnh giác dược"
]

# Patterns for duplicated files
# Pattern 1: ...__c70a15f1.md (8 random hex characters after __)
pattern1 = re.compile(r"__[a-f0-9]{8,}\.md$", re.IGNORECASE)

# Pattern 2: ... 2010bc1073c2815493bdccda01af5459.md (32 hex chars with a preceding space)
pattern2 = re.compile(r" [a-f0-9]{32}.*\.(md|csv)$", re.IGNORECASE)

def cleanup_folder(folder_path):
    if not os.path.exists(folder_path):
        print(f"Không tìm thấy thư mục: {folder_path}")
        return

    count = 0
    size_saved = 0
    for file_name in os.listdir(folder_path):
        if pattern1.search(file_name) or pattern2.search(file_name):
            file_path = os.path.join(folder_path, file_name)
            try:
                size = os.path.getsize(file_path)
                os.remove(file_path)
                print(f"Đã xoá: {file_name}")
                count += 1
                size_saved += size
            except Exception as e:
                print(f"Không thể xoá {file_name}: {e}")
    
    print(f"--- Dọn dẹp xong {folder_path} ---")
    print(f"Xoá tổng cộng: {count} tập tin rác.")
    print(f"Tiết kiệm được: {size_saved / 1024 / 1024:.2f} MB\n")

if __name__ == "__main__":
    print("BẮT ĐẦU DỌN DẸP...\n")
    base_dir = os.path.dirname(os.path.abspath(__file__))
    for d in DATA_DIRS:
        folder_path = os.path.join(base_dir, d)
        cleanup_folder(folder_path)
    print("HOÀN TẤT DỌN DẸP!")