基于DMS文档报告重建文件夹结构并迁移文档需求
基于DMS CSV报告重建文件夹并迁移文件的脚本方案
PowerShell 实现
# 配置路径 $csvPath = ".\文档报告.csv" # 替换为你的CSV文件路径 $sourceDir = ".\导出的文件" # 替换为DMS导出文件的存放目录 $targetRoot = ".\重建的文件夹结构" # 最终生成的文件夹结构根目录 # 读取CSV数据,指定列名 $docs = Import-Csv -Path $csvPath -Header "FolderPath", "FileNameWithoutExt" foreach ($doc in $docs) { # 构建目标文件夹路径并创建(不存在则自动生成) $targetFolder = Join-Path -Path $targetRoot -ChildPath $doc.FolderPath New-Item -ItemType Directory -Path $targetFolder -Force | Out-Null # 查找源目录中所有匹配文件名(忽略扩展名)的文件 $matchingFiles = Get-ChildItem -Path $sourceDir -File | Where-Object { $_.BaseName -eq $doc.FileNameWithoutExt } foreach ($file in $matchingFiles) { $targetFilePath = Join-Path -Path $targetFolder -ChildPath $file.Name Move-Item -Path $file.FullName -Destination $targetFilePath -Force Write-Host "已移动: $($file.Name) → $targetFilePath" } } Write-Host "所有文件迁移完成!"
Python 实现
需要先安装依赖:pip install pandas
import os import shutil import pandas as pd # 配置路径 csv_path = "./文档报告.csv" # 替换为你的CSV文件路径 source_dir = "./导出的文件" # DMS导出文件的存放目录 target_root = "./重建的文件夹结构" # 最终生成的文件夹结构根目录 # 读取CSV数据,指定列名 df = pd.read_csv(csv_path, header=None, names=["FolderPath", "FileNameWithoutExt"]) for _, row in df.iterrows(): folder_path = row["FolderPath"] file_name_no_ext = row["FileNameWithoutExt"] # 创建目标文件夹(不存在则自动生成) target_folder = os.path.join(target_root, folder_path) os.makedirs(target_folder, exist_ok=True) # 遍历源目录匹配文件并移动 for file_name in os.listdir(source_dir): file_full_path = os.path.join(source_dir, file_name) if os.path.isfile(file_full_path): base_name = os.path.splitext(file_name)[0] if base_name == file_name_no_ext: target_file_path = os.path.join(target_folder, file_name) shutil.move(file_full_path, target_file_path) print(f"已移动: {file_name} → {target_file_path}") print("所有文件迁移完成!")
注意事项
- 操作前请备份所有文件,避免意外丢失数据
- 如果CSV文件编码异常,PowerShell可添加
-Encoding UTF8参数到Import-Csv命令;Python可在pd.read_csv中添加encoding='utf-8'参数 - 若存在同名不同扩展名的文件,脚本会将所有匹配文件移动到对应文件夹,可根据需求调整匹配逻辑
- 请将脚本中的路径替换为你的实际文件/目录路径
内容的提问来源于stack exchange,提问作者jmo
相关产品推荐
相关产品推荐

