读取1.xlsx,
A列存入要移动的文件(不包含扩展名)
B列设置要跳过的文件夹
C列设置要跳过的文件名
D列设置要跳过的关键词,文件名包含这些关键词就跳过
最后输出移动日志
import osimport shutilimport pandas as pdfrom datetime import datetime# 获取当前时间,用于保存结果Excel的文件名time = str(datetime.now().strftime("%Y%m%d%H%M%S"))print(time)# 读取Excel文件excel_data = pd.read_excel('1.xlsx')files_to_move = excel_data.iloc[:, 0].dropna().tolist() # 要移动的文件列表folders_to_skip = excel_data.iloc[:, 1].dropna().tolist() # 要跳过的文件夹列表files_to_keep = excel_data.iloc[:, 2].dropna().tolist() # 要保留的文件列表strings_to_keep = excel_data.iloc[:, 3].dropna().tolist() # 包含这些字符串的文件要保留# 初始化记录moved_files = []skipped_files = []not_found_files = []# 移动文件的函数def move_files(src_folder, dest_folder):for root, dirs, files in os.walk(src_folder):# 跳过需要跳过的文件夹dirs[:] = [d for d in dirs if d not in folders_to_skip]for file in files:file_name, file_extension = os.path.splitext(file)if any(s in file_name for s in strings_to_keep):print(f"Skipped due to substring match: {file_name}")skipped_files.append(file_name)continueif file_name in files_to_move and file_name not in files_to_keep:src_path = os.path.join(root, file)relative_root = os.path.relpath(root, src_folder)dest_path = os.path.join(dest_folder, relative_root, file)# 确保目标目录存在os.makedirs(os.path.join(dest_folder, relative_root), exist_ok=True)shutil.move(src_path, dest_path)print(f"Moved: {src_path} to {dest_path}")moved_files.append(file_name)else:skipped_files.append(file_name)# 执行文件移动move_files(r'F:\备份\桌面\图片迁移\全部图片备份-2023年10月26日 - 副本', r'F:\备份\桌面\图片迁移\迁移到这')# 记录未找到的文件not_found_files = list(set(files_to_move) - set(moved_files))# 导出到Excelresult_df = pd.DataFrame({'Moved Files': pd.Series(moved_files),'Skipped Files': pd.Series(skipped_files),'Not Found Files': pd.Series(not_found_files)})# 保存结果到Excelresult_df.to_excel('move_result' + time + '.xlsx', index=False)print('本次迁移已完成')import os import shutil import pandas as pd from datetime import datetime # 获取当前时间,用于保存结果Excel的文件名 time = str(datetime.now().strftime("%Y%m%d%H%M%S")) print(time) # 读取Excel文件 excel_data = pd.read_excel('1.xlsx') files_to_move = excel_data.iloc[:, 0].dropna().tolist() # 要移动的文件列表 folders_to_skip = excel_data.iloc[:, 1].dropna().tolist() # 要跳过的文件夹列表 files_to_keep = excel_data.iloc[:, 2].dropna().tolist() # 要保留的文件列表 strings_to_keep = excel_data.iloc[:, 3].dropna().tolist() # 包含这些字符串的文件要保留 # 初始化记录 moved_files = [] skipped_files = [] not_found_files = [] # 移动文件的函数 def move_files(src_folder, dest_folder): for root, dirs, files in os.walk(src_folder): # 跳过需要跳过的文件夹 dirs[:] = [d for d in dirs if d not in folders_to_skip] for file in files: file_name, file_extension = os.path.splitext(file) if any(s in file_name for s in strings_to_keep): print(f"Skipped due to substring match: {file_name}") skipped_files.append(file_name) continue if file_name in files_to_move and file_name not in files_to_keep: src_path = os.path.join(root, file) relative_root = os.path.relpath(root, src_folder) dest_path = os.path.join(dest_folder, relative_root, file) # 确保目标目录存在 os.makedirs(os.path.join(dest_folder, relative_root), exist_ok=True) shutil.move(src_path, dest_path) print(f"Moved: {src_path} to {dest_path}") moved_files.append(file_name) else: skipped_files.append(file_name) # 执行文件移动 move_files(r'F:\备份\桌面\图片迁移\全部图片备份-2023年10月26日 - 副本', r'F:\备份\桌面\图片迁移\迁移到这') # 记录未找到的文件 not_found_files = list(set(files_to_move) - set(moved_files)) # 导出到Excel result_df = pd.DataFrame({ 'Moved Files': pd.Series(moved_files), 'Skipped Files': pd.Series(skipped_files), 'Not Found Files': pd.Series(not_found_files) }) # 保存结果到Excel result_df.to_excel('move_result' + time + '.xlsx', index=False) print('本次迁移已完成')