7

我正在使用 Powershell 并试图强制复制文件夹/文件而不删除现有目标文件夹中的任何额外文件。我被困在试图获得一个有效的命令。

下面是我的代码,关于如何解决这个问题的任何建议?

Copy-Item -Force -Recurse  –Verbose $releaseDirectory -Destination $sitePath 
4

2 回答 2

6

你需要确保

$realeseDirectory 

是这样的

c:\releasedirectory\*

Copy-item永远不要删除目标中的额外文件或文件夹,但-force如果文件已经存在,它将覆盖

于 2013-02-11T19:29:11.287 回答
1

你的问题不是很清楚。因此,您可能需要稍微调整一下下面的功能。顺便说一句,如果您尝试部署网站,复制目录并不是最好的方法。

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
于 2013-02-11T19:47:49.317 回答