File size: 3,127 Bytes
3856d3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import argparse

def remove_kemono_ids(root_dir):
    """

    Recursively scans the directory and renames folders by removing 

    patterns like ' [123456]' or '[123456]' from the end of their names.

    """
    if not os.path.isdir(root_dir):
        print(f"Error: The directory '{root_dir}' does not exist.")
        return

    # Pattern: optional space, followed by '[', then 1 or more digits, then ']'
    # We use $ to make sure it's at the end of the folder name, or we can just 
    # replace it anywhere. It's safer to just replace it anywhere if it matches.
    # Pattern explanation: \s* matches spaces, \[\d+\] matches [12345]
    pattern = re.compile(r'\s*\[\d+\]\s*$')

    # We walk bottom-up so that renaming a parent folder doesn't break the paths for its children
    renamed_count = 0
    
    for dirpath, dirnames, filenames in os.walk(root_dir, topdown=False):
        # Rename files first
        for filename in filenames:
            name, ext = os.path.splitext(filename)
            if pattern.search(name):
                new_name = pattern.sub('', name).strip() + ext
                
                if not new_name or new_name == ext:
                    continue
                    
                old_path = os.path.join(dirpath, filename)
                new_path = os.path.join(dirpath, new_name)
                
                if not os.path.exists(new_path):
                    try:
                        os.rename(old_path, new_path)
                        print(f"Renamed file: '{filename}' -> '{new_name}'")
                        renamed_count += 1
                    except Exception as e:
                        print(f"Failed to rename file '{filename}': {e}")

        # Rename folders
        for dirname in dirnames:
            if pattern.search(dirname):
                new_name = pattern.sub('', dirname).strip()
                
                if not new_name:
                    continue

                old_path = os.path.join(dirpath, dirname)
                new_path = os.path.join(dirpath, new_name)

                if os.path.exists(new_path):
                    print(f"Cannot rename folder '{dirname}' to '{new_name}' because the destination already exists.")
                    continue

                try:
                    os.rename(old_path, new_path)
                    print(f"Renamed folder: '{dirname}' -> '{new_name}'")
                    renamed_count += 1
                except Exception as e:
                    print(f"Failed to rename folder '{dirname}': {e}")

    print(f"\nScan complete. Total folders renamed: {renamed_count}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Remove Kemono post IDs (e.g. '[12345]') from folder names.")
    parser.add_argument("path", nargs='?', default=".", help="The root directory to scan (default is current directory)")
    
    args = parser.parse_args()
    
    print(f"Starting to scan: {os.path.abspath(args.path)}\n")
    remove_kemono_ids(args.path)