简体   繁体   English

将文件列表保存到文件慢

[英]Saving file list to file slow

This post is provided to hopefully save anyone saving FileInfo object to file without taking excessive time.提供这篇文章是为了希望节省任何将 FileInfo object 保存到文件的人,而不会花费过多时间。

I am building tool to sync files between USB and network storage using robocpy.我正在构建工具以使用 robocpy 在 USB 和网络存储之间同步文件。 Normally files that don't exist on target would be copied and that don't exist on source would be deleted (with /PURGE or /MIR).通常,目标上不存在的文件将被复制,而源上不存在的文件将被删除(使用 /PURGE 或 /MIR)。 I want to be able to do two way sync, so need to track files that previously were in the set and if missing from either side remove them from the other.我希望能够进行双向同步,因此需要跟踪以前在集合中的文件,如果任何一方丢失,请将它们从另一方删除。 This emulates software such as FreeFileSync, but can only be done in Powershell due OS restrictions for us.这模拟了 FreeFileSync 等软件,但由于我们的操作系统限制,只能在 Powershell 中完成。

To track the files, I am dumping FileInfo object from Get-ChildItem <Path> -Recurse -File to CSV file.为了跟踪文件,我将 FileInfo object 从Get-ChildItem <Path> -Recurse -File -File 转储到 CSV 文件。 The FileInfo object contains 12,000+ objects, and was taking over 3 mins to save to file. FileInfo object 包含 12,000 多个对象,保存到文件需要 3 分钟。 Note the time was not due to file write, even using ConvertTo-XML memory object took over 3 mins.请注意,时间不是因为文件写入,即使使用ConvertTo-XML memory object 也需要 3 分钟。

Searching returned a lot of info about Get-ChildItem being slow and recommending .NET EnumerateFiles, but nothing about converting to XML/JSON being slow.搜索返回了很多关于 Get-ChildItem 很慢并推荐 .NET EnumerateFiles 的信息,但没有关于转换为 XML/JSON 很慢的信息。 My GCI completes in under 5 seconds which is fine for our requirements, it is the conversion (specifically VersionInfo) that takes the extra 3 mins.我的 GCI 在 5 秒内完成,这符合我们的要求,但转换(特别是 VersionInfo)需要额外的 3 分钟。 I will look at EnumerateFiles, as do like to squeeze the best performance out of my scripts.我会看看 EnumerateFiles,因为我想从我的脚本中挤出最好的性能。 :) :)

Using select FullName,Length,LastWriteTime reduced the save time to under 3 seconds.使用select FullName,Length,LastWriteTime将保存时间减少到 3 秒以下。 Further investigation identified it was the ScriptProperty VersionInfo that was causing the slowdown.进一步调查确定是 ScriptProperty VersionInfo导致了减速。 Using select with all other FileInfo properties resulted in a save time still under 3 seconds.select与所有其他 FileInfo 属性一起使用,保存时间仍低于 3 秒。

Hope this helps someone.希望这可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM