我想使用Powershell强制复制文件夹/文件,而不删除现有目标文件夹中的额外文件:

我想使用Powershell强制复制文件夹/文件,而不删除现有目标文件夹中的额外文件:,powershell,Powershell,我正在使用Powershell,正在尝试强制复制文件夹/文件,而不删除现有目标文件夹中的任何额外文件。我一直在试图得到一个有效的命令 下面是我的代码,有没有关于如何解决这个问题的建议 Copy-Item -Force -Recurse –Verbose $releaseDirectory -Destination $sitePath 你需要确保 $realeseDirectory 有点像 c:\releasedirectory\* Copy item永远不要删除目标中的额外文件或文件

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

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

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

你需要确保

$realeseDirectory 
有点像

c:\releasedirectory\*

Copy item
永远不要删除目标中的额外文件或文件夹,但使用
-force
如果文件已经存在,它将自动写入您的问题不是很清楚。因此,您可能需要稍微调整下面的函数。顺便说一句,如果您试图部署网站,复制目录不是最好的方法

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

您好C.B.$releaseDirectory=$BuildFilePath+$ProjectName+“\”+$ProjectName+“\bin\”+$compileMode+“\U PublishedWebsites\”+$ProjectName$sitePath=“\\$strSvr\C$\Shared\WebSites”当前它正在删除目标上的额外文件。@user2062422我不能说是否正确构建了路径,但要复制源中的所有文件,您需要在源路径的末尾添加
\*
。。这种行为真的很奇怪。。。使用某些测试路径在本地进行测试。。。您将不会看到额外的文件被删除..您好C.B.-路径正确。当前源有c:\test\file1、file2,目标有c:\shared\website\file1、file2和file3。它应该覆盖文件1和文件2,并保留文件3。它正在覆盖文件1、文件2和删除file3@user2062422嗯。。从来没有过这种行为。。你做过我之前告诉你的测试吗?是的。Releasedirectory现在有\*和路径。当前源有c:\test\file1、file2,目标有c:\shared\website\file1、file2和file3。它应该覆盖文件1和文件2,并保留文件3。它正在覆盖文件1、文件2和删除文件3