Files
novafarma/scripts/flatten_subfolders.py
2026-01-20 01:05:17 +01:00

81 lines
2.4 KiB
Python

import os
import shutil
ROOT = "assets/slike"
def flatten_directory(target_dir):
# Moves all files from subfolders to target_dir, then removes subfolders
print(f" 🔨 Flattening {target_dir}...")
moved = 0
# Walk topdown=False to handle nesting
for root, dirs, files in os.walk(target_dir, topdown=False):
if root == target_dir:
continue
for file in files:
src = os.path.join(root, file)
dst = os.path.join(target_dir, file)
# Handle collision
if os.path.exists(dst):
base, ext = os.path.splitext(file)
c = 1
while os.path.exists(os.path.join(target_dir, f"{base}_{c}{ext}")):
c += 1
dst = os.path.join(target_dir, f"{base}_{c}{ext}")
try:
shutil.move(src, dst)
moved += 1
except Exception as e:
print(e)
# Try remove dir
try:
os.rmdir(root)
except:
pass
return moved
def main():
print("🚀 FLATTENING SUBFOLDERS (Keeping Main Categories)...")
# 1. Standard Categories
cats = ["teren", "liki", "predmeti", "animations", "glavna_referenca"]
for c in cats:
p = os.path.join(ROOT, c)
if os.path.exists(p):
n = flatten_directory(p)
print(f" -> Flattened {c}: {n} files moved.")
# 2. Biomes (Flatten individually)
biomes_root = os.path.join(ROOT, "biomi")
if os.path.exists(biomes_root):
for b in os.listdir(biomes_root):
bp = os.path.join(biomes_root, b)
if os.path.isdir(bp):
n = flatten_directory(bp)
print(f" -> Flattened Biome {b}: {n} files moved.")
# 3. DEMO/FAZA (Flatten individually)
phases = ["DEMO", "FAZA_1", "FAZA_2"]
for ph in phases:
php = os.path.join(ROOT, ph)
if os.path.exists(php):
# Inside Phase, flatten the categories (e.g. FAZA_1/teren)
for sub in os.listdir(php):
subp = os.path.join(php, sub)
if os.path.isdir(subp):
n = flatten_directory(subp)
print(f" -> Flattened {ph}/{sub}: {n} files moved.")
print("="*40)
print("✅ FLATTEN COMPLETE.")
print("="*40)
if __name__ == "__main__":
main()