我经常需要在驱动器之间复制大量文件,这个过程经常会启动和停止。在我可以使用的posix shell中
不覆盖现有文件,但似乎没有等效的“不覆盖”开关
在powershell中。
这意味着如果我必须停止和启动进程,我必须使用
ls -Recurse|%{
if (-Not(test-path($_fullname.replace("D:\", "E:\")))){
cp $_.fullname $_.fullname.replace("D:\", "E:\");
}
}工作得很好,但是如果我有一百万个文件要复制,我会认为必须执行一些开销
每次都是。
编辑:顺便说一句,我试过了
,但是扫描已经存在的文件需要花费很长时间。如果我使用上面的脚本,并且我正在进行一个大会话的一半,那么在它开始复制新文件之前会有几秒钟的停顿;使用robocopy时,它甚至在开始复制之前就已经花了几分钟的时间来运行已经复制的文件。
发布于 2016-03-12 18:39:28
另一种方法是使用
这将在目标存在时抛出一个异常,但随后您必须处理异常处理+创建目录的开销,因此它肯定不会有太大帮助。
您可以使用.NET
方法直接减少路径测试中的powershell开销(2/3)。我没有包装
在函数中添加-calls,因为这会增加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,4492812https://stackoverflow.com/questions/35955289
复制相似问题