locked
Using measure-object to determine size of a folder (Property "length" cannot be found) RRS feed

  • Question

  • I am have trouble getting the file size of a particular folder.

    I am trying to do the following:

    ls "\\someserver\c$\myfolder" | Measure-Object -Property length -sum
    
    Measure-Object : Property "length" cannot be found in any object(s) input.
    At line:1 char:35
    + ls "\\someserver\c$\myfolder" | Measure-Object -Property length -sum
    +                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
        + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

    This folder is NOT empty and this method works on many other folders\servers. If I try using -ErrorAction SilentlyContinue it doesn't return anything. The only thing I can think of that is different about this folder is that it is a DFSR folder.

    When a run the following to check each subfolder I see the error on a few subfolders (non of which are empty):

    $Folders = Get-ChildItem '\\someserver\c$\myfolder'
    $sum = $null
    foreach ($Folder in $Folders)
        {
        $Folder.fullname
        ls $Folder.fullname | Measure-Object -Property length -sum #-ErrorAction SilentlyContinue
        #$sum += $sum
        }
    
    
    \\someserver\c$\myfolder\folder1
    
    
    Count    : 21
    Average  : 
    Sum      : 794925
    Maximum  : 
    Minimum  : 
    Property : Length
    
    \\someserver\c$\myfolder\folder2
    Measure-Object : Property "length" cannot be found in any object(s) input.
    At line:6 char:27
    +     ls $Folder.fullname | Measure-Object -Property length -sum #-ErrorAction Sil ...
    +                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
        + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
    \\someserver\c$\myfolder\folder3
    Count    : 12
    Average  : 
    Sum      : 723851
    Maximum  : 
    Minimum  : 
    Property : length
    
    \\someserver\c$\myfolder\folder4
    Measure-Object : Property "length" cannot be found in any object(s) input.
    At line:6 char:27
    +     ls $Folder.fullname | Measure-Object -Property length -sum #-ErrorAction Sil ...
    +                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
        + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
    \\someserver\c$\myfolder\folder5
    Count    : 6
    Average  : 
    Sum      : 15849403
    Maximum  : 
    Minimum  : 
    Property : Length
    
    \\someserver\c$\myfolder\folder6
    Count    : 2
    Average  : 
    Sum      : 253061
    Maximum  : 
    Minimum  : 
    Property : Length

    Wednesday, March 20, 2013 3:49 PM

Answers

  • This is also an approach. It depends on what you are trying to do.

    dir c:\windows -recurse | ?{!$_.PsIsContainer} | Measure-Object -sum length


    ¯\_(ツ)_/¯

    • Marked as answer by red888 Wednesday, March 20, 2013 5:25 PM
    Wednesday, March 20, 2013 4:03 PM

All replies

  • Try this:

    (get-item c:\windows).GetFiles()|Measure-Object -sum length

    Works every time.

    Folder objects do not have a length and will throw errors.  GetFiles retruns only files in the folder object.  Get-Item gets the folder as an object.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, March 20, 2013 4:01 PM
    • Proposed as answer by Mindaugas Šimkus Thursday, September 3, 2015 12:40 PM
    Wednesday, March 20, 2013 4:00 PM
  • This is also an approach. It depends on what you are trying to do.

    dir c:\windows -recurse | ?{!$_.PsIsContainer} | Measure-Object -sum length


    ¯\_(ツ)_/¯

    • Marked as answer by red888 Wednesday, March 20, 2013 5:25 PM
    Wednesday, March 20, 2013 4:03 PM
  • Your method is awesome and I will remember it for the future and it seems to work on every other folder I try, but on this particular folder it returns nothing.

    example on a different folder gives the the correct results:

    (Get-Item "\\someserver\c$\SomeOtherFolder").getfiles() | Measure-Object -sum length
    
    
    Count    : 3
    Average  :
    Sum      : 5899
    Maximum  :
    Minimum  :
    Property : Length

    But if I run: (Get-Item "\\someserver\c$\MyFolder").getfiles() | Measure-Object -sum length

    I get no out-put or errors. 

    Again the only other thing "special" about this folder is that it is a DFSR folder (but I don't see how this would be any different)

    Wednesday, March 20, 2013 4:11 PM
  • More info:

    I can browse in windows explorer to \\someserver\c$ and check the folder properties of Myfolder which reports the size of the folder.

    Also, while I want to do this by accessing the folder via UNC, even when I remote into the server directly and run:

    [someserver]: PS C:\> (Get-Item ".\MyFolder").getfiles() | Measure-Object -sum length

    I still get no out-put

    • Edited by red888 Wednesday, March 20, 2013 4:18 PM
    Wednesday, March 20, 2013 4:14 PM
  • it seems that .getfiles() gets only files contained inside the specific folder BUT NOT THOSE CONTAINED IN ANY OF ITS SUBFOLDERS!

    I suspect that your folder contains only other folders but no files of its own.

    If you want the size of all files contained within a folder those within all of its subfolders, there is no object for that tree, so you will need to use the other suggestion jrv provided:

        dir c:\windows -recurse | ?{!$_.PsIsContainer} | Measure-Object -sum length

    This, unfortunately, can be time-consuming. I have a report that I create using this method to determine the number and volume of files of large net app shares. One that contains 35,000 files totalling about 6TB takes a half day to run.

    Believe it or not, it is a good thing that folder objects do not maintain this information for their subfolders. If they did, then you could not move files to subfolders to get some relief from the fact that the time it takes to work with a folder increases dramatically as the number of files contained increases. It would also mean that any file created/modified/deleted would require some update to each folder in its path.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, March 20, 2013 4:43 PM
  • More info:

    I can browse in windows explorer to \\someserver\c$ and check the folder properties of Myfolder which reports the size of the folder.

    Also, while I want to do this by accessing the folder via UNC, even when I remote into the server directly and run:

    [someserver]: PS C:\> (Get-Item ".\MyFolder").getfiles() | Measure-Object -sum length

    I still get no out-put

    I can run this remotely both ways and I get the file size sum.

    If you do not have list and transit access then you will see no files.  If you are not using NTFS you will not get a sum.  If this is the system drive then it has to be NTFS for Vista and later,.  For XP I believe we can still choose FAT or FAT32.


    ¯\_(ツ)_/¯

    Wednesday, March 20, 2013 4:50 PM
  •  dir c:\windows -recurse | ?{!$_.PsIsContainer} | Measure-Object -sum length

    This was all I needed. Thanks a lot. 

    "I suspect that your folder contains only other folders but no files of its own."

    This is correct.

    • Proposed as answer by Marco Zuidam Thursday, March 5, 2015 10:25 AM
    Wednesday, March 20, 2013 5:25 PM
  • thanks for voting my comment as helpful. The issue, as is often the case, comes down to communications in English and the sometimes different assumptions that are made. Add to that the fact that the behaviour and properties of computer-based concepts are often expressed in natural-language terminology to make them accessible, at a simple level, while hiding subtle ambiguities at a detailed level.

    We all understand what you meant by the "size of a folder" - unfortunately, we did not all understand the same concept.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, March 20, 2013 9:39 PM
  • Try this:

    (get-item c:\windows).GetFiles()|Measure-Object -sum length

    Works every time.

    Folder objects do not have a length and will throw errors.  GetFiles retruns only files in the folder object.  Get-Item gets the folder as an object.


    ¯\_(ツ)_/¯


    Output shows folder size where there's none. As an example, an empty folder shows up in GUI as having a size of "0" KB, yet all empty folders show up as 0.1MB, instead of "0" that was reported by Get-ChildItem. Furthermore, some folders that are NOT empty are also reported with same size - 0.1MB or 101,376 (bits?). 

    Any ideas why?



    • Edited by Cynep Friday, July 19, 2013 5:01 PM size
    Friday, July 19, 2013 4:54 PM
  • What GUI?

    The output of Measure-Object is not in Mb so your issue is very strange.


    ¯\_(ツ)_/¯

    Friday, July 19, 2013 5:14 PM