You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本开发需求:对比两文件夹并复制差异文件至第三文件夹

文件夹差异文件同步实现方案

需求概述

  • 有两个子目录结构完全一致的文件夹:SourceFolder和DestinationFolder,二者文件内容相似;另有ResultsFolder用于存放差异文件
  • 需完成两个核心任务:
    1. 将SourceFolder中存在但DestinationFolder没有的新文件,复制到ResultsFolder对应的子目录中
    2. 将SourceFolder与DestinationFolder中同名但内容不同的文件,复制到ResultsFolder对应的子目录中

场景示例

SourceFolder和DestinationFolder的目录结构如下:

SourceFolder\sub1 → file1sub1.txt、file2sub1.txt
SourceFolder\sub2 → file1sub2.txt
SourceFolder\sub3 → file1sub3.txt

DestinationFolder\sub1 → file1sub1.txt
DestinationFolder\sub2 → file1sub2.txt
DestinationFolder\sub3 → file1sub3.txt

预期执行效果:

  • 在ResultsFolder下创建sub1子目录,复制file2sub1.txt到ResultsFolder\sub1
  • 若file1sub2.txt内容有变更,在ResultsFolder下创建sub2子目录并复制该文件
  • file1sub3.txt无内容差异,无需任何操作

现有脚本问题分析

你提供的测试脚本存在以下几个关键问题:

  1. SideIndicator判断使用了HTML转义字符&lt;=,实际应为<=,导致无法正确匹配源文件夹的差异文件
  2. 复制文件时仅使用$_.Name作为源路径,丢失了原文件的子目录结构,会把所有文件直接复制到ResultsFolder根目录
  3. Copy-Item的参数误用,单个文件复制不需要-Recurse和-Container参数

修正后的PowerShell脚本

$SourceFolder = 'C:\Test\Source\'
$DestinationFolder = 'C:\Test\Destination\'
$ResultsFolder = 'C:\Test\Results\'

# 获取源文件夹所有文件,包含完整路径、相对路径和MD5哈希
$RefFiles = Get-ChildItem $SourceFolder -Recurse -File | 
    Select-Object FullName, Name,
        @{Name = 'RelativePath'; Expression = $_.FullName.Substring($SourceFolder.Length) },
        @{Name = 'Hash'; Expression = (Get-FileHash $_.FullName -Algorithm MD5).Hash }

# 获取目标文件夹所有文件的名称和MD5哈希
$DiffFiles = Get-ChildItem $DestinationFolder -Recurse -File | 
    Select-Object Name,
        @{Name = 'Hash'; Expression = (Get-FileHash $_.FullName -Algorithm MD5).Hash }

# 对比筛选源文件夹独有的或内容变更的文件,复制到结果目录
Compare-Object -ReferenceObject $RefFiles -DifferenceObject $DiffFiles -Property Name, Hash |
    Where-Object { $_.SideIndicator -eq '<=' } |
    ForEach-Object {
        # 构建结果文件夹中的目标路径
        $targetPath = Join-Path -Path $ResultsFolder -ChildPath $_.RelativePath
        # 确保目标目录存在
        $targetDir = Split-Path -Path $targetPath -Parent
        if (-not (Test-Path -Path $targetDir)) {
            New-Item -Path $targetDir -ItemType Directory -Force | Out-Null
        }
        # 复制文件到目标位置
        Copy-Item -Path $_.FullName -Destination $targetPath -Force
    }

关键优化点

  • 新增RelativePath属性,保留文件在源文件夹中的子目录结构,确保复制到ResultsFolder时路径与源目录一致
  • 修正SideIndicator判断逻辑,正确识别源文件夹中新增或内容变更的文件
  • 自动创建目标子目录,避免因目录不存在导致复制失败
  • 使用文件完整路径定位源文件,确保复制操作准确无误

内容的提问来源于stack exchange,提问作者John McClane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:57:46