Files
novafarma/scripts/cleanup_filenames.py

54 lines
2.1 KiB
Python

import os
import re
import shutil
TARGET_DIR = os.path.abspath("assets/slike/glavna_referenca")
def clean_filenames():
print("🧹 Cleaning filenames in glavna_referenca (removing timestamps)...")
count = 0
for filename in os.listdir(TARGET_DIR):
if filename.startswith("."): continue
if os.path.isdir(os.path.join(TARGET_DIR, filename)): continue
# Regex to find "_176xxxxxxxxx" pattern (timestamp) and trailing numbers
# Pattern: look for "_17" followed by digits, possibly followed by "_1" etc.
# Example: sink_1767540048311_1.png -> sink.png
name, ext = os.path.splitext(filename)
# 1. Remove timestamp (long number starting with 176 or 177)
new_name = re.sub(r'_17[67]\d{7,}', '', name)
# 2. Remove trailing "_1", "_2", "_nobg" if desired, but let's keep variants if meaningful.
# Let's just remove trailing "_1" leftovers from copies
new_name = re.sub(r'_\d+$', '', new_name)
# 3. Handle "MOJE_SLIKE_KONCNA_ostalo_" garbage prefix
new_name = new_name.replace("MOJE_SLIKE_KONCNA_ostalo_", "")
new_name = new_name.replace("src_assets_library_godot_sprites_source_", "")
if new_name != name:
new_filename = new_name + ext
src = os.path.join(TARGET_DIR, filename)
dst = os.path.join(TARGET_DIR, new_filename)
# Avoid overwriting if simplified name exists (append counter)
if os.path.exists(dst):
# Keep the one we are renaming as a variant? Or overwrite?
# Safety: Counter
c = 1
while os.path.exists(os.path.join(TARGET_DIR, f"{new_name}_{c}{ext}")):
c+=1
dst = os.path.join(TARGET_DIR, f"{new_name}_{c}{ext}")
os.rename(src, dst)
count += 1
# print(f" RENAME: {filename} -> {os.path.basename(dst)}")
print(f"✨ Cleaned {count} filenames!")
if __name__ == "__main__":
clean_filenames()