Files
KomGrabber/src/logic/move_test.py
2025-02-27 22:41:38 +01:00

46 lines
1.4 KiB
Python

import os
import re
folder_path = "/home/alexander/Downloads/torrents/Manga_test/"
def rename(folder):
"""Rename the files in a folder according to the template.
Template: [Name] v[nr] #[nr].ext (e.g. "The Flash v1 #1.cbz").
Args:
----
- folder (str): the string to the folder
"""
# Get the files in the folder
files = os.listdir(folder)
for file in files:
if not file.endswith(".cbz"):
print(f"Skipping {file}, not a cbz file")
continue
ext = file.split(".")[-1]
match = re.search(r"v\d{2,4} ", file)
if match:
print(match)
split_start = match.start()
split_end = match.end()
# Split the filename between split_start and split_end
volume = file[split_start:split_end]
# Split the filename at the split index, but keep the "v" and digits in the title
title = file[:split_start].strip()
# add the volume number to the title as a suffix #nr
title = f"{title} {volume} #{volume.replace('v','')}"
print(title)
# rename the file
os.rename(f"{folder}/{file}", f"{folder}/{title}.{ext}")
for folder in os.listdir(folder_path):
if os.path.isdir(f"{folder_path}/{folder}"):
rename(f"{folder_path}/{folder}")
print(f"Renamed {folder}")
else:
print(f"{folder} is not a folder")
continue