使用Get-ChildItem时出现PathTooLongException错误

我正尝试使用属性ArchiveOffline生成文件共享上所有已知文件的列表。

但是我一直在PathTooLongException错误。

到目前为止,我只发现Robocopy可以处理文件长度错误,除了我不能获得相同types的“ 仅这些属性 ”类别的结果。

例:

 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这样的列表的方式吗?

Robocopy是瑞士军刀,有很多select。 我认为所有其他的工具都会受到path太长的错误的影响。 你真的需要Get-ChildItem在CSV中产生的所有属性,还是只对全限定文件名列表感兴趣? 如果是后者,试试这个:

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

/ L产生一个没有复制数据的列表。
/ FP包含完整path而不仅仅是文件名。
/ NP抑制日志中的%进度消息
请参阅Robocopy用法中的“文件select选项”:
/ IA:[RASHCNETO] ::只包含任何给定属性集的文件

在这个答案中find了一个基于Powershell的解决scheme,它解释了如何忽略这些令人畏惧的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的口味被认为“太长”的所有path (就像命令所承诺的那样)。 然而,在我自己的情况下,我只是要遏制这些关于node_modules文件夹的错误。 其他人也可能在这里遇到同样的问题,所以我决定在这里分享……