Initial commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import shutil
|
||||
import re
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python rename_copies.py <directory_path>")
|
||||
sys.exit(1)
|
||||
|
||||
work_dir = sys.argv[1]
|
||||
target_subdir = os.path.join(work_dir, "Copies annotées")
|
||||
|
||||
# Create destination folder if it doesn't exist
|
||||
os.makedirs(target_subdir, exist_ok=True)
|
||||
|
||||
# Regex to match "CopieXX.json" and capture XX
|
||||
pattern = re.compile(r"^Copie(\d+)\.json$")
|
||||
|
||||
for filename in os.listdir(work_dir):
|
||||
match = pattern.match(filename)
|
||||
if match:
|
||||
copie_id = match.group(1)
|
||||
json_path = os.path.join(work_dir, filename)
|
||||
source_folder = os.path.join(work_dir, f"Anot_Copie{copie_id}")
|
||||
|
||||
# Check if corresponding folder exists
|
||||
if os.path.isdir(source_folder):
|
||||
try:
|
||||
with open(json_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
name = data.get("name", "Unknown").strip()
|
||||
|
||||
# Sanitize filename (remove characters invalid in paths)
|
||||
safe_name = re.sub(r'[<>:"/\\|?*]', '', name)
|
||||
|
||||
new_folder_name = f"{safe_name} ({copie_id})"
|
||||
dest_path = os.path.join(target_subdir, new_folder_name)
|
||||
|
||||
print(f"Moving '{source_folder}' -> '{dest_path}'")
|
||||
shutil.move(source_folder, dest_path)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing {filename}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user