输出的复制项目“预览”

时间:2018-08-08 20:39:19

标签: powershell

有关PowerShell Copy-Item命令的快速问题。我想知道您是否具有目录结构,并且想要覆盖另一个目录结构,是否可以在“预览”模式下运行Copy-Item命令。它会将覆盖的文件从目录a输出到目录b,但实际上不执行Copy-Item命令。

感谢任何帮助或建议。

谢谢。

3 个答案:

答案 0 :(得分:1)

tl;博士

  • Copy-Item -WhatIf不会为您提供所需的详细信息级别-参见下文。

  • 改为使用robocopy.exe -l (仅限Windows),如Ansgar Wiechers所建议的,因为它会单独列出要复制的文件 ,包括动态忽略目标目录中已经存在的那些目录(默认情况下具有相同的大小和最后修改的时间戳)。


Get-Help about_CommonParameters文档许多(但不是全部)cmdlet支持的-WhatIf公用参数,其目的是预览操作而没有实际执行。

但是,此功能是以抽象方式实现的,通常不会提供人们希望的详细信息。

关键点:尽管Copy-Item支持-WhatIf,但它可能无法为您提供所需的详细程度,因为如果源项目是目录< / em>,仅输出行,如下所示:

What if: Performing the operation "Copy Directory" on target "Item: sourceDir Destination: destDir".

请注意,无论您的Copy-Item呼叫是否包含-Recurse开关,您都会看到同一行。

即使您手动确保目标目录的存在并将/*附加到源目录路径以查看各个文件名,您也只会在 child 级别看到它们,无需深入子树,您将永远无法获得robocopy -l提供的有关实际需要替换哪些文件的动态信息。

答案 1 :(得分:1)

有趣的问题! 这是我在Powershell中进行的所有尝试,因此不需要RoboCopy。

function Copy-Preview {
    [CmdletBinding(DefaultParameterSetName = 'ByPath')]
    param(
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = 'ByPath', Position = 0)]
        [ValidateScript({ Test-Path $_ })]
        [string]$Path,

        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = 'ByLiteralPath', Position = 0)]
        [ValidateScript({ Test-Path $_ })]
        [string]$LiteralPath,

        [Parameter(ValueFromPipeline = $true, Mandatory = $true, Position = 1)]
        [string]$Destination,

        [string]$Filter = $null,
        [string]$Include = $null,
        [string]$Exclude = $null,
        [switch]$Recurse,
        [switch]$Force
    )

    if ($PSCmdlet.ParameterSetName -eq 'ByLiteralPath') { $Path = $LiteralPath }

    # determine if $Path and $Destination hold a file or a directory
    $srcIsFolder = (Test-Path $Path -PathType Container -ErrorAction SilentlyContinue)
    # cannot use Test-Path here because then the path has to exist.
    # assume if it has an extension the path is a file, otherwise a directory
    # NOTE:
    # This is certainly not fullproof, so to avoid problems, always make sure 
    # the destination ends with a backslash if a directory is intended.
    $destIsFolder = (-not ([System.IO.Path]::HasExtension($Destination)))

    if ($destIsFolder -and !(Test-Path $Destination -PathType Container)) {
        Write-Host "Destination path does not exist yet. All files from '$Path' will be copied fresh" -ForegroundColor Green
        return
    }
    elseif ($srcIsFolder -and (!$destIsFolder)) {
        # should not happen: source is a directory, while the destination is a file..
        Write-Error "When parameter Path points to a directory, the Destination cannot be a file.."
        return
    }

    $count = 0
    if ($srcIsFolder -and $destIsFolder) {
        # Both the source and the destinations are folders
        # make sure both paths are qualified for .Replace() further down
        if (-not $Path.EndsWith("\")) { $Path += "\" }
        if (-not $Destination.EndsWith("\")) { $Destination += "\" }

        $splat = @{
            Filter  = $Filter
            Include = $Include
            Exclude = $Exclude
            Recurse = $Recurse
            Force   = $Force
        }
        # add either Path or LiteralPath to the parameters as they are mutually exclusive
        if ($PSCmdlet.ParameterSetName -eq 'ByPath') { $splat.Path = $Path }
        else { $splat.LiteralPath = $LiteralPath }

        $srcFiles  = Get-ChildItem @splat | Select-Object -ExpandProperty FullName

        # reuse the splat parameters hash for the destination, but change the Path
        if ($splat.LiteralPath) {($splat.Remove("LiteralPath"))}
        $splat.Path = $Destination

        $destFiles = Get-ChildItem @splat | Select-Object -ExpandProperty FullName

        foreach ($srcItem in $srcFiles) { 
            $destItem = $srcItem.Replace($Path, $Destination)
            if ($destFiles -contains $destItem) {
                Write-Host "'$destItem' would be overwritten"
                $count++
            }
        }
    }
    elseif (!$srcIsFolder) {
        # the source is a file
        if (!$destIsFolder) {
            # the destination is also a file
            if (Test-Path $Destination -PathType Leaf) {
                Write-Host "'$Destination' would be overwritten"
                $count++
            }
        }
        else {
            # source is file, destination is a directory
            $destItem = Join-Path $Destination (Split-Path $Path -Leaf)
            if (Test-Path $destItem -PathType Leaf) {
                Write-Host "'$destItem' would be overwritten"
                $count++
            }
        }
    }

    $msg = "$count item{0} would be overwritten by Copy-Item" -f $(if ($count -ne 1) { 's' })
    $dash = "-" * ($msg.Length)
    Write-Host "$dash`r`n$msg" -ForegroundColor Green
}

答案 2 :(得分:0)

您可以使用-whatif参数。

Copy-item -path myfile.txt -destination myfoldertoCopyTo\ -whatif