PoshCode Archive  Artifact [5d60f3556d]

Artifact 5d60f3556ddadb88245979adbd9cd241369b6cf8d01b3288c40c01ff7ab4b4d7:

  • File Copy-from-source-to-destination-.ps1 — part of check-in [aeb6d407d0] at 2018-06-10 12:58:25 on branch trunk — Copy from source to destination and rename existing destination files to .old, if .old exists replace it. (user: unknown size: 889)

# encoding: ascii
# api: powershell
# title: 
# description: Copy from source to destination and rename existing destination files to .old, if .old exists replace it.
# version: 0.1
# license: CC0
# x-poshcode-id: 1551
# x-archived: 2009-12-25T16:58:02
#
#
param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Destination)

Get-ChildItem -Path $Path | Where-Object { !$_.PSIsContainer } | foreach {
	$Target = Join-Path -Path $Destination -ChildPath (Split-Path -Leaf $_)
	if ( Test-Path -Path $Target -PathType Leaf ) {
		$PrevTargetBkup=([System.IO.Path]::ChangeExtension($Target, ".old"))
		if ( Test-Path -Path $PrevTargetBkup -PathType Leaf ) {
			Remove-Item -Force -Path $PrevTargetBkup
		}
		Rename-Item -Path $Target -NewName ([System.IO.Path]::ChangeExtension($Target, ".old"))
	}
	Copy-Item -Path $_ -Destination $Target
}