Skip to content

Instantly share code, notes, and snippets.

@patsbin
Created January 30, 2019 07:12
Show Gist options
  • Select an option

  • Save patsbin/9f9cae00cc02dc6d0ed36a8bd880e52b to your computer and use it in GitHub Desktop.

Select an option

Save patsbin/9f9cae00cc02dc6d0ed36a8bd880e52b to your computer and use it in GitHub Desktop.
Compare and copy files in directories
<#
.SYNOPSIS
Compare and copy files in directories
.DESCRIPTION
Compared files in folder a (leftdir) to files in folder b (rightdir).
If the file is not in folder b and a third directory is defined, the file will be copied there.
See Get-Help .\CompareFolders.ps1 -Examples
.EXAMPLE
CompareFolders.ps1 D:\localfonts D:\originalFonts D:\AddedFonts
#>
Param(
[Parameter(Mandatory=$True)]
[string]$leftdir,
[Parameter(Mandatory=$True)]
[string]$rightdir,
[Parameter(Mandatory=$False)]
[string]$destinationdir
)
Write-host "Left directory: $leftdir"
Write-host "Right directory: $rightdir"
If ($destinationdir)
{
Write-host "Write added files to: $destinationdir"
if(!(Test-Path -Path $destinationdir ))
{
Write-host "Destination directory doesn't exist. Creating..."
New-Item -ItemType directory -Path $destinationdir
}
} Else
{
Write-host "No third destination directory defined."
}
$a = Get-ChildItem -Path $rightdir -Recurse
$b = Get-ChildItem -Path $leftdir -Recurse
$differentfiles = Compare-Object -ReferenceObject $a -DifferenceObject $b
If ($destinationdir)
{
foreach ($file in $differentfiles.InputObject.FullName)
{
Write-host "Copy file: $file"
Copy-Item $file -Destination $destinationdir
}
} Else
{
Write-host "Listing:"
$differentfiles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment