【问题标题】:Using Powershell I want to Forcefully Copy Folder/files Without Erasing Extra Files in Existing Destination Folder:使用 Powershell 我想强制复制文件夹/文件而不删除现有目标文件夹中的额外文件:
【发布时间】:2013-02-12 03:47:49
【问题描述】:

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

以下是我的代码,有什么建议可以解决这个问题吗?

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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    你需要确定

    $realeseDirectory 
    

    有点像

    c:\releasedirectory\*
    

    Copy-item 永远不会删除目标中的额外文件或文件夹,但使用-force,如果文件已存在,它会 owwrite

    【讨论】:

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

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

    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:\test\file 1,file2,目标有 c:\shared\website\file1、file2 和 file3。它应该覆盖 file1 和 file2 并且应该保留 file3。它正在覆盖file1,file2并删除file3
    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多