gpt4 book ai didi

powershell - 使用 Robocopy 获取文件列表

转载 作者:行者123 更新时间:2023-12-03 12:30:38 25 4
gpt4 key购买 nike

我正在尝试查看使用 Robocopy 的解决方案是否比使用 Get-ChildItem 获取给定文件夹(和子文件夹...)内的文件列表更快。

在我的代码中,我使用 Get-ChildItem cmdlet 来获取特定文件夹内所有文件的列表,以便循环访问这些文件中的每一个:

$files = Get-ChildItem "C:\aaa" -Recurse | where {! $_.PIsContainer} # ! because I don't want to list folders
foreach ($file in $files){
...
}

现在,我有 robocopy 命令来获取所有文件的列表,但是,robocopy 的输出是一个字符串。

[string]$result = robocopy "C:\aaa" NULL /l /s /ndl /xx /nc /ns /njh /njs /fp

那么,我如何使用 robocopy 命令的输出在每个文件上循环(类似于使用 Get-ChildItem 所做的?

最佳答案

如果您只是想寻找一种更快的方法来获取该文件列表,旧的 dir 命令可以做到这一点:

$files = cmd /c dir c:\aaa /b /s /a-d
foreach ($file in $files){
...
}

编辑:一些比较性能测试-

(measure-command {gci -r |? {-not $_.psiscontainer } }).TotalMilliseconds
(measure-command {gci -r -file}).TotalMilliseconds
(measure-command {(robocopy . NULL /l /s /ndl /xx /nc /ns /njh /njs /fp) }).TotalMilliseconds
(measure-command {cmd /c dir /b /s /a-d }).TotalMilliseconds

627.5434
417.8881
299.9069
86.9364

测试目录有420个子目录下的6812个文件。

关于powershell - 使用 Robocopy 获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28353925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com