folder size in bytes

Welcome Forums General PowerShell Q&A folder size in bytes

This topic contains 6 replies, has 5 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #118542

    Participant
    Points: 64
    Rank: Member

    I have the following to get the folder size in bytes, but when I run the script it says

    ELAND 143520842837 bytes

    But, when I go to the actual folder, and go to properties, it says the size is 143550466575 bytes. Why are they different? Which one is more accurate?

    https://www.gngrninja.com/script-ninja/2016/5/24/powershell-calculating-folder-sizes

    $DCS_location = '\\172.16.100.100\JetStor\DCS'
    #$folders = Get-ChildItem -Path $DCS_location | Where-Object {$_.Extension -eq ""}
    $folders = Get-ChildItem -Path $DCS_location | Where-Object {$_.name -eq "ELAND"}
    
    foreach ($f in $folders) {
    
    $folder_full_name = Get-ChildItem $f.FullName -recurse
    $folder_size = ($folder_full_name | Measure-Object -property length -sum).Sum
    
    Write-Host $f.Name $folder_size "bytes"
    
    }
  • #118545

    Participant
    Points: 316
    Helping Hand
    Rank: Contributor

    Is is a static file? A log file that is being written to? Regardless of the difference between the file system, does it need to accurate down to the byte?

    PS C:\Users\Rob> 143520842837 / 1gb
    133.664200861938
    
    PS C:\Users\Rob> 143550466575 / 1gb
    133.691790117882
    
    PS C:\Users\Rob> [int](143520842837 / 1gb)
    134
    
    PS C:\Users\Rob> [int](143550466575 / 1gb)
    134
    
  • #118548

    Participant
    Points: 5
    Rank: Member

    Most likely it is the difference between size of bytes which is what you get in Length but then there is the "Size on Disk" which is the sum of the block size like 2 files that total 2.8K in 2 files but on disk it is 8K because the drive was formatted in 4K blocks.

  • #118549
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    It works for me. Length agrees with Size:, but not "Size on disk:". I had to do "get-childitem -force" to get any hidden files. System files don't seem to give it any trouble.

    # find hidden files
    ls -r -force | where mode -match h
    
  • #118554

    Participant
    Points: 870
    Helping Hand
    Rank: Major Contributor

    but the delta here is very small, it wont be because of 4k formating. I think its because hidden files.

    @Tony , can you give -force for Get-childItem and measure the sum, it treats hidden files as well.

  • #118587

    Participant
    Points: 64
    Rank: Member

    I'm trying with the -Force now.  I didn't think about it.

  • #118590

    Participant
    Points: 64
    Rank: Member

    Thank you, after adding

    -Force

    , it's matching with the folder properties.

You must be logged in to reply to this topic.