由于深度超过255个字符而无法获取最大文件的总和?

问题描述

当目录路径超过255个字符且文件超过大约300万个时,如何获得以下脚本的结果?

 (Get-ChildItem N:\Shared Folder\Replicated Files -recurse | Sort-Object length -descending | select-object -first 32 | measure-object -property length -sum).sum /1gb 

我得到的错误:

Get-ChildItem : Could not find a part of the path 'N:\Shared Folder\Replicated Files...'.
At line:1 char:3
+  (Get-ChildItem N:\Shared Folder\Replicated Files -recurse | Sort-Object length -de ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (N:\Shared Folder\Replicated Files) [Get-ChildItem],DirectoryNotFoundException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

解决方法

robocopy命令支持超过255个字符的文件路径。您可以使用\L参数仅将找到的文件(及其大小)列出到输出流中。添加一些其他参数以清理输出(例如,\BYTES将大小显示为字节-有关全部内容的说明,请参见robocopy help

这为您提供了以下命令,以列出所有文件及其大小(以字节为单位):

robocopy /L /E /ndl /njh /njs /bytes "N:\Shared Folder\Replicated Files" nocopy

为您提供如下输出:

        New File                     154        C:\Temp\file1.tmp
        New File                 1878884        C:\Temp\file2.tmp
        New File                  119465        C:\Temp\file3.tmp

然后您可以使用正则表达式解析出文件大小,并使用您的方法进行排序和添加,我们得到了:

(robocopy /L /E /ndl /njh /njs /bytes "N:\Shared Folder\Replicated Files" nocopy | %{if($_ -match "New File\W*(\d*)\W*([\w:\\\.]*)"){[int32]$matches[1]}} | sort -Descending | select -first 32 | measure -sum | select -expand Sum) / 1gb

不太优雅,但是应该可以。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...