Skip to content

Instantly share code, notes, and snippets.

@pollusb
Last active March 20, 2018 20:09
Show Gist options
  • Save pollusb/cd47b4afeda8edbf8943a8808c880eb8 to your computer and use it in GitHub Desktop.
Save pollusb/cd47b4afeda8edbf8943a8808c880eb8 to your computer and use it in GitHub Desktop.
Copy-IfNotPresent
Function Copy-IfNotPresent {
<#
.SYNOPSIS
Copy file only if not present at destination.
.DESCRIPTION
This is a one file at a time call. It's not meant to replace complex call like ROBOCOPY.
Destination can be a file or folder.
If it's a folder, you can use -Container to force Folder creation when not exists.
If it's a file, you can provide a different name.
.LINK
https://gist.github.com/pollusb/cd47b4afeda8edbf8943a8808c880eb8
.AUTHOR
Pollus Brodeur
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
$FilePath,
[Parameter(Mandatory)]
[string]$Destination,
[switch]$Container,
[switch]$WhatIf
)
#region validations
if ($FilePath -isnot [System.IO.FileInfo]){
$File = Get-ChildItem $FilePath -File
} else {
$File = $FilePath
}
if (!$File.Count){
Write-Warning "$FilePath no file found."
return
} elseif ($File.Count -gt 1) {
Write-Warning "$FilePath must resolve to one file only."
return
}
#endregion
# Destination is a folder
if ($Container -or (Test-Path -Path $Destination -PathType Container)) {
if (!(Test-Path $Destination)) {
New-Item -Path $Destination -ItemType Container | Out-Null
Write-Verbose "Copy-IfNotPresent $Destination (is absent) creating folder"
}
$Destination += "\$($File.Name)"
}
# Destination is a file
if (!(Test-Path $Destination)) {
if ($WhatIf) {
Write-Host "WhatIf:Copy-IfNotPresent $FilePath -> $Destination"
} else {
# Force creation of parent folder
$Parent = Split-Path $Destination -Parent
if (!(Test-Path $Parent)) {
New-Item $Parent -ItemType Container | Out-Null
Write-Verbose "Copy-IfNotPresent $Parent (is absent) creating folder"
}
Copy-Item -Path $FilePath -Destination $Destination
Write-Verbose "Copy-IfNotPresent $FilePath -> $Destination (is absent) copying"
}
} else {
Write-Verbose "Copy-IfNotPresent $Destination (is present) not copying"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment