<# FPS:Containers:Common:Public:Move-SingleFile:ACTIVE
#>
function Move-SingleFile {
<#
.DESCRIPTION
Module: Common
Public CmdLet:
Move-SingleFile.ps1
Dependencies: None
The CmdLet will either move or
copy one file to another location. If a
file exists in the destination directory,
-KeepTarget can be used to
rename that file to a numeric sub (file_1.txt).
It will not bother any files in the destination
directory unless they are in the original directory. If additional directories are present that
are not in the original directory
tree, those directories will remain. If the directory or any of its subs that are
in the original directory are not present on the
destination directory, those additional directories will
be created in the destination.
.USAGE
Called throughout FPS to
move/copy directories. Typical usage will be within a public or private module.
String print of action if
verbose.
Move-SingleFile
-SourceFile <String[]>
-TargetFile <String[]>
-KeepSource <String['YES','NO']>
-KeepTarget <String['YES','NO']>
[-Verbose]
.EXAMPLE
Will copy the
from file to the to location. In this example, if an existing
Common-Next-1.ps1 file is there, it will rename the
file to
Pro-Config_1.ps1 and move the copied one into the new Pro-Config.ps1's place.
if (Push-NeededFor Move-SingleFile) { .
(Get-ActiveFileNm -PS1 Move-SingleFile)
Move-SingleFile
-SourceFile 'D:\FPS\Metadata\Pro-Config.ps1' -TargetFile 'C:\FPS\Metadata\Pro-Config.ps1' -KeepSource 'YES' -KeepTarget
'YES'
EXAMPLE:
Will copy the
from file to the to location. If an existing Pro-Config.ps1 file is there,
it will removed and move the copied one
into the new
Pro-Config.ps1's place.
if (Push-NeededFor Move-SingleFile) { .
(Get-ActiveFileNm -PS1 Move-SingleFile)
Move-SingleFile
-SourceFile 'D:\FPS\Metadata\Pro-Config.ps1' -TargetFile 'C:\FPS\Metadata\Pro-Config.ps1' -KeepSource 'YES' -KeepTarget 'NO'
EXAMPLE:
Will move the from file one location to another, replacing any existing
match files in the destination.
if (Push-NeededFor Move-SingleFile) { .
(Get-ActiveFileNm -PS1 Move-SingleFile)
Move-SingleFile
-SourceFile 'D:\FPS\Metadata\Pro-Config.ps1' -TargetFile 'C:\FPS\Metadata\Pro-Config.ps1' -KeepSource 'NO' -KeepTarget 'NO'
.CC
[2024.09.25 JD] Original code release
.NOTES
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)] [string]$SourceFile,
[Parameter(Position=1, Mandatory=$true)] [string]$TargetFile,
[Parameter(Position=2, Mandatory=$true)] [string]$KeepSource,
[Parameter(Position=3, Mandatory=$true)] [string]$KeepTarget
)
if (Test-Path $TargetFile) {
if ($KeepTarget.ToUpper() -eq 'YES') {
if ($PSBoundParameters.Verbose -eq $true) { Write-Verbose ("Keeping original file "+$TargetFile) }
$RenamePart=$TargetFile.Split('.')
$RenameFileNum=1
$RenameFileNm=$RenamePart[0]+'_1'+'.'+$RenamePart[1]
while (Test-Path $RenameFileNm) {
$RenameFileNum++
$RenameFileNm=$RenamePart[0]+'_'+[string]$RenameFileNum+'.'+$RenamePart[1]
}
if ($PSBoundParameters.Verbose -eq $true) { Write-Verbose ("Copy-Item "+$TargetFile+"
-Destination "+$RenameFileNm) }
Copy-Item $TargetFile -Destination $RenameFileNm
Remove-Item -Path $TargetFile -Force
} else {
if ($PSBoundParameters.Verbose -eq $true) { Write-Verbose ("Removing original file "+$TargetFile) }
Remove-Item -Path $TargetFile -Force
}
}
if ($PSBoundParameters.Verbose -eq $true) { Write-Verbose ("Copy-Item "+$SourceFile+"
-Destination "+$TargetFile) }
Copy-Item $SourceFile -Destination $TargetFile
if ($KeepSource.ToUpper() -eq 'NO') { Remove-Item -Path $SourceFile -Force }
}