import os
import shutil
from pathlib import Path


# 将原始的四份文件转移到下载文件夹
def move_raw_files(folder_before, folder_after, save_folder):
    try:
        # 确保目标文件夹存在
        Path(folder_after).mkdir(parents=True, exist_ok=True)

        # 文件格式
        extensions = (".docx", ".xlsx", ".xls")

        # 创建子文件夹
        if not os.path.exists(f"{folder_after}/{save_folder}"):
            os.makedirs(f"{folder_after}/{save_folder}", exist_ok=True)

        # 遍历源文件夹
        file_paths = []
        for item in os.listdir(folder_before):
            item_path = os.path.join(folder_before, item)

            # 只处理文件(不处理子文件夹)且扩展名匹配
            if os.path.isfile(item_path) and item.lower().endswith(extensions):
                # 构造目标路径
                target_path = os.path.join(f"{folder_after}/{save_folder}", item)

                # 移动文件
                shutil.move(item_path, target_path)
                file_paths.append(f"{folder_after}/{save_folder}/{item}")

                print(f"已移动: {item}")

        print(f"\n移动完成! 共移动了 {len(file_paths)} 个文件到 {folder_after}")
        print(f"文件路径: {file_paths}")
        return file_paths

    except Exception as e:
        print(f"原始文件移动失败: {e}")


if __name__ == "__main__":
    folder_before = r"E:\code\python_code\daily_work\backend\temp_uploads"
    folder_after = r"E:\code\python_code\daily_work\backend\temp_download_raw"
    move_raw_files(folder_before, folder_after, "202505")
