Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Powershell I want to Forcefully Copy Folder/files Without Erasing Extra Files in Existing Destination Folder:

Tags:

powershell

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 
like image 646
user2062422 Avatar asked Feb 11 '13 19:02

user2062422


2 Answers

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

like image 137
CB. Avatar answered Oct 22 '22 06:10

CB.


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
like image 25
David Brabant Avatar answered Oct 22 '22 07:10

David Brabant