80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
|
|
import os
|
|
import shutil
|
|
|
|
ROOT = "assets/slike"
|
|
|
|
def flatten_folder_content_to_self(folder_path):
|
|
# Moves all files from ANY subfolder of folder_path into folder_path
|
|
# Then deletes the subfolders
|
|
print(f" 🔨 Flattening {folder_path}...")
|
|
|
|
if not os.path.exists(folder_path):
|
|
return 0
|
|
|
|
moved = 0
|
|
# Walk bottom-up
|
|
for root, dirs, files in os.walk(folder_path, topdown=False):
|
|
if root == folder_path:
|
|
continue
|
|
|
|
for file in files:
|
|
src = os.path.join(root, file)
|
|
dst = os.path.join(folder_path, file)
|
|
|
|
# Collision
|
|
if os.path.exists(dst):
|
|
base, ext = os.path.splitext(file)
|
|
c = 1
|
|
while os.path.exists(os.path.join(folder_path, f"{base}_{c}{ext}")):
|
|
c += 1
|
|
dst = os.path.join(folder_path, f"{base}_{c}{ext}")
|
|
|
|
try:
|
|
shutil.move(src, dst)
|
|
moved += 1
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
# Remove dir
|
|
try:
|
|
os.rmdir(root)
|
|
# print(f" Deleted {root}")
|
|
except:
|
|
pass
|
|
|
|
return moved
|
|
|
|
def main():
|
|
print("🚀 FLATTENING EVERYTHING INSIDE TOP-LEVEL FOLDERS...")
|
|
|
|
# Get all immediate children of assets/slike
|
|
# e.g. DEMO, FAZA_1, biomi, teren...
|
|
|
|
top_level = [d for d in os.listdir(ROOT) if os.path.isdir(os.path.join(ROOT, d))]
|
|
|
|
for folder in top_level:
|
|
full_path = os.path.join(ROOT, folder)
|
|
|
|
# Special Case: 'biomi'
|
|
# We don't want to flatten 'biomi' itself (mixing Biomes).
|
|
# We want to flatten 'biomi/Farm', 'biomi/Desert'.
|
|
if folder == "biomi":
|
|
print(" 🌍 Processing Biomes (Flattening per Biome)...")
|
|
for b in os.listdir(full_path):
|
|
bp = os.path.join(full_path, b)
|
|
if os.path.isdir(bp):
|
|
n = flatten_folder_content_to_self(bp)
|
|
print(f" -> {b}: {n} moved.")
|
|
else:
|
|
# Flatten 'Teren', 'Liki', 'DEMO', 'FAZA_1', 'glavna_referenca'
|
|
n = flatten_folder_content_to_self(full_path)
|
|
print(f" -> {folder}: {n} moved.")
|
|
|
|
print("="*40)
|
|
print("✅ ABSOLUTE FLATTENING COMPLETE.")
|
|
print("="*40)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|