使用 Get-ChildItem 时出现 PathTooLongException 错误

使用 Get-ChildItem 时出现 PathTooLongException 错误

我正在尝试生成文件共享上所有已知文件的列表,其中包含属性ArchiveOffline

但我总是遇到PathTooLongException错误。

到目前为止,我发现 Robocopy 可以解决文件长度错误,但我无法得到同样的“这些属性仅“之类的结果。

例子:

Get-ChildItem -Attributes A+O -Recurse |Export-Csv E:\temp\StubSearchCorp.csv

给出:

Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be 
less than 248 characters.
At line:1 char:1
+ Get-ChildItem -Attributes A+O -Recurse |Export-Csv E:\temp\StubSearchA.Corp.cs ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : ReadError: (F:\Corporate\...e A Train & Ship:String) [Get-ChildItem], PathTooLongException
   + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

有人知道如何将这样的列表导出到 CSV 吗?

答案1

Robocopy 是一把拥有众多选项的瑞士军刀。我认为所有其他工具都会遇到路径过长错误。您真的需要 Get-ChildItem 在 CSV 中生成的所有属性吗,还是您只对完全限定文件名列表感兴趣?如果是后者,请尝试以下操作:

ROBOCOPY source dest /IA:AO /FP /NP /S /L /LOG:myfiles.txt

/L 生成列表而不复制数据。/FP
包含完整路径,而不仅仅是文件名。/NP
隐藏日志中的 % 进度消息。
请参阅 Robocopy 用法中的“文件选择选项”:
/IA:[RASHCNETO] :: 仅包含具有任何给定属性集的文件

答案2

在以下位置找到了基于 Powershell 的解决方案这个答案,解释了如何忽略这些可怕的PathTooLongException日志记录。以下是我的具体用法:

Get-ChildItem *.txt -Recurse -ErrorAction SilentlyContinue | Select-String "my pattern"

对于 OP 的情况,这相当于:

Get-ChildItem -Attributes A+O -Recurse -ErrorAction SilentlyContinue | Export-Csv "rslt.csv"

一个主要缺点:这将默认忽略所有被认为“太长”的路径适合 Powershell 的口味(正如命令所承诺的那样)。但是,就我自己而言,我只是想抑制这些有关node_modules文件夹的错误。其他人可能会遇到同样的问题,所以我决定在这里分享……

相关内容