powershell 比较两个文件夹,复制差异并将旧文件清理到第二个文件夹中

powershell 比较两个文件夹,复制差异并将旧文件清理到第二个文件夹中

要求:

我有2个文件夹A和B。

文件夹 A 包含新旧文件以及目录和子目录中的一些变化。

文件夹B中有几个旧文件以及目录和子目录。

我需要比较两个文件夹 A 和 B 的内容,并将所有新的或修改的文件和目录从文件夹 A 复制到文件夹 B,并在文件夹 B 中删除文件夹 A 中没有的所有旧文件。

# Files in 1 but not in 2 should wind up in 3 with the same dir structure as occurs in 1
param(
    $Folder1path = "C:\Folder1",
    $Folder2path = "C:\Folder2"
)

$ErrorActionPreference = "Stop";
Set-StrictMode -Version 'Latest'

Get-ChildItem -Path $Folder1Path -Recurse | Where-Object {

    [string] $toDiff = $_.FullName.Replace($Folder1path, $Folder2path)
    # Determine what's in 2, but not 1
    [bool] $isDiff = (Test-Path -Path $toDiff) -eq $false

    if ($isDiff) {
        # Create destination path that contains folder structure
        $dest = $_.FullName.Replace($Folder1path, $Folder2path)
        Copy-Item -Path $_.FullName -Destination $dest -Verbose -Force
    }
}

在此处输入代码

答案1

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null

$dirpath = 'C:\'
$FolderName=[Microsoft.VisualBasic.Interaction]::InputBox('Enter a Folder Name','Folder Name')

$FolderName = "$dirpath"+$FolderName
if(!(Test-Path -Path $FolderName)){
    New-Item -ItemType Directory -Path $FolderName
    New-Item -ItemType Directory -Path $FolderName\OLD
    New-Item -ItemType Directory -Path $FolderName\NEW
    Write-Output "File '$FolderName' created successfully"
} else {
 
    [System.Windows.MessageBox]::Show("File '$FolderName' already exists")
}

Read-Host -Prompt "Copy New .exe files in [$folderName\NEW] and Press any key to continue..."


$RefBase  = "C:\123\NEW"   #  'D:\apps\newfiles\'
$DiffBase = "C:\Test"   #  '\\share\Existingfiles\'
$Target   = "C:\123\OLD"   #  '\\share2\backup\'

$RefTree = Get-ChildItem $RefBase -File -Recurse | Select-Object Length,
           @{n='SubName';e={$_.FullName.Replace($RefBase,'')}}

$DiffTree =  Get-ChildItem $DiffBase -File -Recurse | Select-Object Length,
           @{n='SubName';e={$_.FullName.Replace($DiffBase,'')}}

Compare-Object -ReferenceObject  $RefTree `
               -DifferenceObject $DiffTree `
               -Property SubName `
               -ExcludeDifferent `
               -IncludeEqual | Where-Object SideIndicator -eq "==" |
    ForEach-Object {
        $SourceFile = Join-Path  $DiffBase $_.SubName
        $TargetFile = Join-Path  $Target   $_.SubName
        $TargetDir  = Split-Path $TargetFile -Parent
        If (!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
        Copy-Item $SourceFile -Destination $TargetFile -Force
    }


Read-Host -Prompt "Press any key to continue...to copy new .exe in to C:\Test"


$RefBase  = "C:\123\NEW"   #  'D:\apps\newfiles\'
$DiffBase = "C:\123\NEW"   #  '\\share\Existingfiles\'
$Target   = "C:\Test"   #  '\\share2\backup\'

$RefTree = Get-ChildItem $RefBase -File -Recurse | Select-Object Length,
           @{n='SubName';e={$_.FullName.Replace($RefBase,'')}}

$DiffTree =  Get-ChildItem $DiffBase -File -Recurse | Select-Object Length,
           @{n='SubName';e={$_.FullName.Replace($DiffBase,'')}}

Compare-Object -ReferenceObject  $RefTree `
               -DifferenceObject $DiffTree `
               -Property SubName `
               -ExcludeDifferent `
               -IncludeEqual | Where-Object SideIndicator -eq "==" |
    ForEach-Object {
        $SourceFile = Join-Path  $DiffBase $_.SubName
        $TargetFile = Join-Path  $Target   $_.SubName
        $TargetDir  = Split-Path $TargetFile -Parent
        If (!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
        Copy-Item $SourceFile -Destination $TargetFile -Recurse -Force
    }

相关内容