powershell - 复制项目缓慢 - 过滤和复制 5500 个文件需要 40 分钟?

标签 powershell powershell-4.0

我最近编写了一个小脚本,帮助我从包含许多不同子文件夹和文件的较大目录中提取包含大量小 txt 文件的子文件夹。

总而言之,我将包含 txt 文件的约 17GB 文件夹(约 100 个包含约 5,500 个文件的文件夹)从外部硬盘复制到了本地驱动器。连接方式为 USB 2.0。

这花了大约 40 分钟!为什么花了这么长时间?

开销是否更多地与我的代码有关,或者与过时的数据连接的缓慢有关? future 我可以走哪些捷径? robocopy 本质上比 copy-item 更快吗?如果是这样,为什么?

$mainDir = Get-ChildItem 'E:\Some Big Directory\' -Recurse | Where {$_.Mode -match 'd'} | % {$_.FullName}

ForEach($dir in $mainDir){
    if($dir.ToString() -match 'SubfoldersIWant'){
        Copy-Item -Path $($dir + '\TheActualFolderIWant\*') -Destination 'C:\somwhere' -Recurse
    }
}

文件夹结构如下(尺寸缩小):

Some Big Directory
    -SubfoldersIWant1
    -subFolderIDontwant
        -random.txt
    -subFoldersIWant
        -*.txt
    -rando.jpg
    -I dont want this folder
    -random junk
    -SubfoldersIWant2
    -subFolderIDontwant
        -random.txt
    -subFoldersIWant
        -*.txt
    -rando.jpg
    -dontwantthiseither.jpg

最佳答案

您可以使用这一行:

Get-ChildItem 'E:\Some Big Directory' -Directory -Recurse | Where {$_.FullName -match 'SubfoldersIWant'} | Copy-Item -Destination 'C:\somwhere' -Recurse

这样,过滤速度就快得多。但老实说,它不太可能产生很大的影响,因为您的瓶颈似乎是数据传输速率。

关于powershell - 复制项目缓慢 - 过滤和复制 5500 个文件需要 40 分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43369769/

相关文章:

powershell - 如何将哈希表数组转换为字符串数组

variables - PowerShell 参数 - "The term ' 参数'未被识别为 cmdlet 的名称”

windows - 如何使用 PowerShell 下载完整存储库?

powershell - 代码块阻止脚本运行,但以交互方式运行

powershell - 从模块中获取导入脚本的路径?

powershell - 如何使用 PowerShell v4 将成员从一个安全组复制到 AD 中的另一个安全组?

powershell - 带有通配符的奇怪Powershell GCI递归结果

powershell - 在Powershell中切片数组(或列表)的更好方法

通过网络解锁 Windows session

html - 使用Powershell打开本地.htm文件并通过XPath获取值