I am using Powershell and am trying to forcefully copy folder/files without erasing any extra files in existing destination folders. I am stuck trying to get a working command.
Below is my code, any suggestions on how to fix this?
Copy-Item -Force -Recurse –Verbose $releaseDirectory -Destination $sitePath
you need to be sure that
$realeseDirectory
is something like
c:\releasedirectory\*
Copy-item
never delete extra files or folders in destination, but with -force
it will owerwrite if file already exists
Your question isn't very clear. So, you might have to tweak the function below a bit. By the way, if you are attempting to deploy a web site, copying a directory isn't the best way.
function Copy-Directory
{
param (
[parameter(Mandatory = $true)] [string] $source,
[parameter(Mandatory = $true)] [string] $destination
)
try
{
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object { $null = New-Item -ItemType Container -Path $_ }
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { -not $_.psIsContainer } |
Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
}
catch
{
Write-Error "$($MyInvocation.InvocationName): $_"
}
}
$releaseDirectory = $BuildFilePath + $ProjectName + "\" + $ProjectName + "\bin\" + $compileMode + "_PublishedWebsites\" + $ProjectName
$sitePath = "\\$strSvr\c$\Shared\WebSites"
Copy-Directory $releaseDirectory $sitePath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With