Files
ConfigData/rename-media/oldversion/fixmremname.py
CrazyJasonwell 290ff8984f commit
2026-10-07 11:07:00 +08:00

92 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import re
from datetime import datetime
# ====================== 【配置区,请修改】 ======================
MEDIA_FOLDER = "/Volumes/JoyDisk/myMedia/olstrms/电影/未分类/2024"
DRY_RUN = True # True预览不修改;确认后改为False
SKIP_PROCESSED = False
LOG_FILE = "./rename_log.txt"
# =================================================================
RULES = [
(r"^[A-Za-z] ", ""), #1.开头单个字母+空格
(r"\[.*?\]", ""), #2.英文方括号
(r"【.*?】", ""), #2.中文方括号
(r"\b(2160p|1080p|720p|HDR|DV|DoVi|HEVC|x265|x264|DDP|AAC|Atmos|TrueHD|中英双语)\b", "", re.IGNORECASE),
(r"\.+", ""), #4.连续多点删除
(r"\s", ""), #5.删除全部空格
]
ALLOW_EXT = {".mkv", ".mp4", ".mov", ".srt", ".ass", ".sub"}
def clean_name(name: str) -> str:
name_no_ext, ext = os.path.splitext(name)
for item in RULES:
if len(item) == 3:
pattern, repl, flag = item
name_no_ext = re.sub(pattern, repl, name_no_ext, flags=flag)
else:
pattern, repl = item
name_no_ext = re.sub(pattern, repl, name_no_ext)
name_no_ext = re.sub(r"\s+", " ", name_no_ext).strip()
return name_no_ext + ext
def write_log(msg: str):
time_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line = f"[{time_str}] {msg}"
print(line)
with open(LOG_FILE, "a", encoding="utf-8") as f:
f.write(line + "\n")
if __name__ == "__main__":
write_log("===== 文件名+文件夹清洗【递归处理全部下级子目录】 =====")
write_log(f"根目录:{MEDIA_FOLDER}")
write_log(f"预览模式 DRY_RUN = {DRY_RUN}")
write_log("说明:递归扫描全部子目录,先收集所有待改,执行时【先文件、后文件夹】避免路径失效")
dir_rename_list = []
file_rename_list = []
for root, dirs, files in os.walk(MEDIA_FOLDER, topdown=False):
write_log(f"\n==== 扫描目录:{root} | 当前目录文件数量:{len(files)} ====")
# 处理文件
for fname in files:
ext = os.path.splitext(fname)[1].lower()
if ext not in ALLOW_EXT:
continue
new_name = clean_name(fname)
if new_name != fname:
if SKIP_PROCESSED and re.search(r"\(\d{4}\)", new_name):
write_log(f"【跳过文件】{fname},检测年份括号")
continue
old_path = os.path.join(root, fname)
new_path = os.path.join(root, new_name)
file_rename_list.append((old_path, new_path))
write_log(f"【待改文件】{fname} → {new_name}")
# 处理文件夹
for dname in dirs:
new_name = clean_name(dname)
if new_name != dname:
if SKIP_PROCESSED and re.search(r"\(\d{4}\)", new_name):
write_log(f"【跳过文件夹】{dname},检测年份括号")
continue
old_path = os.path.join(root, dname)
new_path = os.path.join(root, new_name)
dir_rename_list.append((old_path, new_name))
write_log(f"【待改文件夹】{dname} → {new_name}")
# 执行顺序:先全部文件,再全部文件夹
file_count = 0
for old, new in file_rename_list:
write_log(f"【执行文件】{os.path.basename(old)} → {os.path.basename(new)}")
file_count +=1
if not DRY_RUN:
os.rename(old, new)
dir_count = 0
for old, new in dir_rename_list:
write_log(f"【执行文件夹】{os.path.basename(old)} → {os.path.basename(new)}")
dir_count +=1
if not DRY_RUN:
os.rename(old, new)
write_log(f"\n任务完成,待修改文件夹:{dir_count},待修改文件:{file_count}")
write_log("="*60 + "\n")