totals of numbers

Welcome Forums General PowerShell Q&A totals of numbers

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

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #119175

    Participant
    Points: 64
    Rank: Member

    I'm trying to calculate the total bytes of two files in the foreach, and I can see that it's displaying the bytes of the two files (171731 and 2317), but the write host $bytes will only display the byte of the last file. How do I total them so the $bytes = 174046?

    do I need to put $bytes=0 before the for() like the counter?

    $data_folder = 'J:\DCS\ELAND\Final\test 1'
    $db_folder = 'J:\DCS\ELAND\Final\test 2'
    $data = Get-ChildItem $data_folder -Recurse -Force
    $db = Get-ChildItem $db_folder -Recurse -Force
    
    $difference = Compare-Object -ReferenceObject $data -DifferenceObject $db        
    $counter = 0
    
    foreach ($d in $difference) {
           $counter++;
           $bytes = 0
           $bytes = (Get-ChildItem $d.InputObject.fullname | Measure-Object -property length -sum).Sum
           $bytes++
    
           $bytes
    }
    Write-Host "`t`t`t$counter files, $bytes bytes in Data directory NOT in DB"
    171731
    2317
    			2 files, 2317 bytes in Data directory NOT in DB
  • #119181

    Participant
    Points: 885
    Helping Hand
    Rank: Major Contributor

    You are resetting $bytes to 0 in every iteration.

    You don't need to do iterations on each differed object.
    You can do it like below.

    $Difference.InputObject | Measure-Object -Property Length -Sum
    
  • #119188

    Participant
    Points: 64
    Rank: Member

    I'm trying to calculate the total bytes of two files in the foreach, and I can see that it's displaying the bytes of the two files (171731 and 2317), but the write host $bytes will only display the byte of the last file. How do I total them so the $bytes = 174046?

    do I need to put $bytes=0 before the for() like the counter?

    PowerShell
    17 lines

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    $data_folder = 'J:\DCS\ELAND\Final\test 1'
    $db_folder = 'J:\DCS\ELAND\Final\test 2'
    $data = Get-ChildItem $data_folder Recurse Force
    $db = Get-ChildItem $db_folder Recurse Force
    $difference = Compare-Object ReferenceObject $data DifferenceObject $db
    $counter = 0
    foreach ($d in $difference) {
    $counter++;
    $bytes = 0
    $bytes = (Get-ChildItem $d.InputObject.fullname | Measure-Object property length sum).Sum
    $bytes++
    $bytes
    }
    Write-Host "`t`t`t$counter files, $bytes bytes in Data directory NOT in DB"
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    PowerShell
    3 lines

    1
    2
    3
    171731
    2317
    2 files, 2317 bytes in Data directory NOT in DB
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

    Thank you, exactly what I was looking for, it works.

You must be logged in to reply to this topic.