Created
January 30, 2019 07:12
-
-
Save patsbin/9f9cae00cc02dc6d0ed36a8bd880e52b to your computer and use it in GitHub Desktop.
Compare and copy files in directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .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