File size: 2,747 Bytes
bc6743c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import unicodedata
import re

def remove_accents(input_str):
    if not input_str:
        return ""
    s1 = unicodedata.normalize('NFD', input_str)
    s2 = ''.join(c for c in s1 if unicodedata.category(c) != 'Mn')
    s3 = re.sub(r'đ', 'd', s2)
    s4 = re.sub(r'Đ', 'D', s3)
    return s4

def rename_files_in_folder(folder_path):
    folder_path = os.path.abspath(folder_path)
    if not os.path.exists(folder_path):
        print(f"Directory not found: {folder_path}")
        return

    files = os.listdir(folder_path)
    print(f"Found {len(files)} files in {folder_path}")

    # Sort files to rename longer names first or just by name to avoid conflicts? 
    # Actually just iterating is fine if we check target existence
    
    for filename in files:
        # Skip directories
        if os.path.isdir(os.path.join(folder_path, filename)):
            continue
            
        # Get name and extension
        name, ext = os.path.splitext(filename)
        
        # Remove accents from name part
        new_name = remove_accents(name)
        
        # Clean up special characters characters that might be unsafe for web
        # Keep alphanumeric, spaces, hyphens, underscores, dots, parenthesis
        new_name = re.sub(r'[^a-zA-Z0-9\s\-\_\(\)\.]', '', new_name)
        
        # Collapse multiple spaces
        new_name = re.sub(r'\s+', ' ', new_name).strip()
        
        new_filename = new_name + ext
        
        if new_filename != filename:
            old_path = os.path.join(folder_path, filename)
            new_path = os.path.join(folder_path, new_filename)
            
            # Handle potential collision
            if os.path.exists(new_path):
                print(f"Skipping (Target exists): {filename} -> {new_filename}")
                continue
                
            try:
                os.rename(old_path, new_path)
                # Print mapping for JSON update manually if needed, or just list later
                # Print using repr to handle potential encoding display issues
                print(f"Renamed: {filename} -> {new_filename}")
            except Exception as e:
                print(f"Error renaming {filename}: {e}")
        else:
            print(f"No change needed: {filename}")

if __name__ == "__main__":
    # Force UTF-8 stdout
    if sys.stdout.encoding.lower() != 'utf-8':
        sys.stdout.reconfigure(encoding='utf-8')
        
    if len(sys.argv) > 1:
        target_folder = sys.argv[1]
    else:
        print("Usage: python rename_files_unsigned.py <folder_path>")
        sys.exit(1)
        
    rename_files_in_folder(target_folder)