相当于 cp -n 的 Powershell,即复制文件而不覆盖

Powershell equivalent of cp -n, i.e. copy files without overwriting

我经常需要在驱动器之间复制大量文件,并且该过程经常启动和停止。在 posix shell 中,我可以使用 cp -n 来不覆盖现有文件,但似乎没有 copy-item 的等效 "do not overwrite" 开关shell.

这意味着如果我必须停止并启动我必须使用的过程

ls -Recurse|%{
    if (-Not(test-path($_fullname.replace("D:\", "E:\")))){
        cp $_.fullname $_.fullname.replace("D:\", "E:\");
    }
}

工作正常,但如果我有一百万个文件要复制(有时会发生),我想每次都必须执行 test-path 会有一些开销。

编辑:顺便说一句,我试过 robocopy.exe d:\thedir e:\thedir /XN /XO /S,但是扫描已经存在的文件花了很长时间。如果我使用上面的脚本并且我在一个大会话中进行了一半,那么在它开始复制新文件之前会有几秒钟的暂停;对于 robocopy,它甚至在开始复制之前就已经花费了几分钟 运行 遍历已复制的文件。

另一种方法是使用 [System.IO.File]::Copy(source,dest),它会在目标存在时抛出异常,但是你必须处理异常处理 + 创建目录的开销,所以它可能不会有太大帮助.

您可以直接使用 .NET Exists() 方法来减少路径测试中的一些 powershell 开销 (2/3)。我没有将 Exists() 调用包装在函数中,因为这会增加 powershell 开销。

#Avoid aliases in scripts. You want people to be able to read it later
Get-ChildItem -Recurse| ForEach-Object {
    if (-Not([System.IO.File]::Exists($_fullname.replace("D:\", "E:\")) -or [System.IO.Directory]::Exists($_fullname.replace("D:\", "E:\")))){
        Copy-Item -Path $_.fullname -Destination $_.fullname.replace("D:\", "E:\")
    }
}

比较:

Measure-Command { 1..100000 | % { [System.IO.File]::Exists("C:\users\frode") -or [System.IO.Directory]::Exists("C:\users\frode") } } | Select-Object -ExpandProperty totalseconds

6,7130002

Measure-Command { 1..100000 | % { Test-Path "C:\users\frode" } } | Select-Object -ExpandProperty totalseconds

22,4492812