Empty folder size value should be set to zero

Welcome Forums General PowerShell Q&A Empty folder size value should be set to zero

  • This topic has 6 replies, 4 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #280206
      Participant
      Topics: 3
      Replies: 7
      Points: 55
      Rank: Member

      We use the following instruction to calculate a folder size

      gci c:\my_folder | measure Length -sum

      However, if the folder is empty, this instruction returns an error.  This happens in PowerShell 5 to PowerShell 7.  The error message is:

      Measure-Object: Cannot process argument because the value of argument “Property” is not valid. Change the value of the “Property” argument and run the operation again.

      IMO, Measure-Object’s behaviour is correct.  So the culprit is the folder object returned by PowerShell.  The size property of folder object should be set to zero by default.

      How to make PowerShell developer take a look at this issue?  Could someone file a report?

      Thanks

    • #280236
      Participant
      Topics: 1
      Replies: 96
      Points: 441
      Helping Hand
      Rank: Contributor

      I cannot reproduce this error with Get-ChildItem as it just returns null. Get-Item does return the error, however, in PowerShell Core. Since Length is a property of files, I’d consider this expected behavior. If you feel the need to report an issue, just go to Github and report it. Personally, I would just code for the condition:

      • #280437
        Participant
        Topics: 3
        Replies: 7
        Points: 55
        Rank: Member

        I don’t know why you can’t reproduce this error.  I get it all the time.  From Win 10 to Windows Server 2016 to 2019.  All the time.

        You said length is a property of file object.  OK.  But that doesn’t mean we can’t add this property to folder object.

        Thanks for your code.  I knew I could make a larger code to cope with this empty folder situation, but that makes the code very cumbersome.  The advantage of using PowerShell is that we could use very compact code to do a lot of works.  If we are forced to use cumbersome, better call the language InfirmityShell rather than PowerShell 😄

        I’ve just filed on Github at https://github.com/PowerShell/PowerShell/issues/14449

        • This reply was modified 1 month ago by 庭豐 石.
      • #280470
        Participant
        Topics: 3
        Replies: 7
        Points: 55
        Rank: Member

        It was not possible to paste image here so I couldn’t show you the error.  But here’s the image:

    • #280503
      Participant
      Topics: 25
      Replies: 208
      Points: 790
      Helping Hand
      Rank: Major Contributor

      just playing around, what you’re running into is gci is returning nothing when the folder is empty, thus there is nothing in the pipeline for the measure cmdlet to act upon.

      get-childitem p|get-member

      you’ll see “nothing” is returned, so you will need to handle the empty folders with a logical check as shown.

      when there are items in your directory you will see all the data return

      get-childitem c:\temp|get-member

      otherwise, if you feel it should work differently, you need to submit a bug on the repo.

    • #280695
      Participant
      Topics: 3
      Replies: 7
      Points: 55
      Rank: Member

      just playing around, …

      Well, you have not been playing enough….. because…

      just playing around, what you’re running into is gci is returning nothing when the folder is empty, thus there is nothing in the pipeline for the measure cmdlet to act upon.

      get-childitem p|get-member

      That’s not true.  The pipeline has an object, but the object has no property called length.

       

      you’ll see “nothing” is returned, so you will need to handle the empty folders with a logical check as shown.

      when there are items in your directory you will see all the data return

      get-childitem c:\temp|get-member

      otherwise, if you feel it should work differently, you need to submit a bug on the repo.

      I have replied this point already to AdminOfThings45.  There’s no point in repeating it.

      <p style=”text-align: left;”>I knew I could make a larger code to cope with this empty folder situation, but that makes the code very cumbersome.  The advantage of using PowerShell is that we could use very compact code to do a lot of works.  If we are forced to use cumbersome, better call the language InfirmityShell rather than PowerShell</p>

    • #280842
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

      Let me see if I can help.  It looks like you have the following structure:

      C:\temp\p\p

      and this directory has nothing in it.  Does this look correct?

      Assuming my test environment matches yours, I think I might understand the issue.  When you run Get-ChildItem against a directory the default view (text returned to the screen) includes a column heading called “Length” but that property only applies to files and NOT directories, so if the directory you run Get-Childitem against only has directories and no files, that column heading will still be present but no values.  To illustrate this I put a simple text file in the .\p directory.

      To further illustrate I’ll pipe the results to Get-Member.  Notice there are two object types displayed in this result System.IO.FileInfo and System.IO.DirectoryInfo.  The FileInfo object has a length property but the DirectoryInfo object does not.

      Note length is the size of the file.  If you’d like to view the sum of size of the files contained in a directory, I can help with that, but windows does not calculate a size (length) for a directory.  This is a Windows thing NOT a PowerShell thing.

      https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.length?view=net-5.0

      • This reply was modified 1 month ago by Mike R..
Viewing 4 reply threads
  • You must be logged in to reply to this topic.