I have a script that get the size of each file in KB, I want to return true only if the delta between them up to 10%
Example1:
$file1 = 100 KB
$file2 = 110 KB
In this case I need to get true
Example2:
$file1 = 90 KB
$file2 = 110 KB
This should return false
Maybe someone can help me with a command that can solve it
CodePudding user response:
Here's a good start to solve your comparison question.
You've got to calculate the different using some math.
#!/usr/bin/env powershell
#get the two files
$hassize = '{0}.name has a size of {1}'
$file1 = Get-Item -Path "$env:HOMEDRIVE/Powershell/Scripts/dchero.ps1"
$file2 = Get-Item -Path "$env:HOMEDRIVE/Powershell/Scripts/findinstaller.ps1"
#measure their individual sizes
Write-Output -InputObject ($hassize -f $file1.name,$file1.Length)
Write-Output -InputObject ($hassize -f $file2.name,$file2.Length)
##calculate the size difference
$diff =[math]::Round( ( 100 * ($file1.Length - $file2.Length) / (($file1.Length - $file2.Length) / 2)),5)
Write-Output -InputObject ("That's a {0}% difference!" -f $diff)
Now that you've calculated the difference you can use an if/else
loop to determine the output.
if ($diff -gt 10) {
## the "10" ^ here represents the number 10 literally
## the input from $diff will be
## an integer that represents a % of change
Write-Output -InputObject ('Turns outs {0} is bigger than 10!' -f $diff)
}
Else {
Write-Output -InputObject ('Turns outs {0} is smaller than 10!' -f $diff)
}
CodePudding user response:
The most basic solution would be something like this:
$file1 = 100KB
$file2 = 110KB
if ($file2 -ge ($file1 * 1.1)) {
"file2 is at least 10% bigger than File1"
}