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
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
[quote quote=119175]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?
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;" spellcheck="false" wrap="off"></textarea>
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
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
171731
2317
2 files, 2317 bytes in Data directory NOT in DB
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
Thank you, exactly what I was looking for, it works.