Senior Systems Engineer
Senior Systems Engineer

Reputation: 1141

Unable to get the sum of largest files due to longer than 255 characters deep?

How can I get the result of the below script when the directory path is longer than 255 characters the files are more than 3 million + approximately?

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

The error I'm getting:

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

Upvotes: 0

Views: 133

Answers (1)

zdan
zdan

Reputation: 29450

The robocopy command supports file paths longer than 255 characters. You can use the \L argument to only list the files found (and their sizes) to the output stream. Adding a few other arguments to clean up the output (for example \BYTES to display size as bytes - see the robocopy help for an explanation of them all)

That gives you the following command to get a listing of all the files and their sizes in bytes:

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

which gives you output that looks like this:

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

You can then use a regex to parse out the file sizes and sorting and adding using your method, we get this:

(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

Not very elegant, but it should work.

Upvotes: 2

Related Questions