PowerShell - 比较 2 种大小的文件和 return 仅当变化高达 10% 时才为真

PowerShell - Compare between 2 sizes of files and return true only if the change is up to 10%

我有一个脚本可以获取每个文件的大小(以 KB 为单位),我想 return 仅当它们之间的差异达到 10%(向上或向下)时才为真

示例 1:

$file1 = 100 KB
$file2 = 110 KB

在这种情况下,我需要得到 true

示例 2:

$file1 = 90 KB
$file2 = 110 KB

这应该return false

到目前为止我得到了什么:

$size1 = ($file1 = Get-Item -Path 'C:\Intel\file1.sql').length/1024 
$size2 = ($file2 = Get-Item -Path 'C:\Intel\file2.sql').length/1024


$DifferenceCount = $size1 - $size2 +1
$percentageDifference = $DifferenceCount / $size1 * 100

    if(($percentageDifference -ge 10) -or ($percentageDifference -le -10))
{
    Write-Host "Percentage difference is bigger than +/-10%"
}
else {write-host "Percentage difference is less than +/-10%"}

以 % 为单位计算差异的正确公式是什么?

这是解决比较问题的良好开端。

你必须使用一些数学来计算差异。

#!/usr/bin/env powershell

#get the two files
$hassize = '{0}.name has a size of {1} KB'
$file1 = Get-Item -Path "$env:HOMEDRIVE/test.csv"
$file2 = Get-Item -Path "$env:HOMEDRIVE/jedurham_login_events.csv"

#measure their individual sizes
Write-Output -InputObject ($hassize -f $file1.name,($file1.Length/1KB))
Write-Output -InputObject ($hassize -f $file2.name,($file2.Length/1KB))

##calculate the size difference
$diff =[math]::Round(($file1.Length - $file2.Length) / ($file1.Length) * 100)
Write-Output -InputObject ("That's a {0}% difference!" -f $diff)

现在您已经计算出差异,您可以使用 if/else 循环来确定输出。

    #!/usr/bin/env powershell

#get the two files
$hassize = '{0}.name has a size of {1} KB'
$file1 = Get-Item -Path "$env:HOMEDRIVE/test.csv"
$file2 = Get-Item -Path "$env:HOMEDRIVE/jedurham_login_events.csv"

#measure their individual sizes
Write-Output -InputObject ($hassize -f $file1.name,($file1.Length/1KB))
Write-Output -InputObject ($hassize -f $file2.name,($file2.Length/1KB))

##calculate the size difference
$diff =[math]::Round(($file1.Length - $file2.Length) / ($file1.Length) * 100)
Write-Output -InputObject ("That's a {0}% difference!" -f $diff)

if ($diff -gt 10)
{
    ####################################################################################
    ##
    ## the above is using the '-gt' comparison operation
    ## to check if $diff is greater than 10%
    ##
    ## this is part of the script where we would define
    ## what we want to happen if the comparison is evaluatd as true
    ## or in this case
    ##
    ## if the percentage of difference between file1 and file2 is GREATER THAN 10
    ##
    ## the calculation being used is 
    ##
    ## percentage difference = ((a - b) / (a) * 100) 
    ##



    Write-Output -InputObject ('The percentage difference is {0}. Which is bigger than 10!' -f $diff)

    ## Move-Item -Path $file1 -Destination 'path/if/true'

}
Else{

    ## this is where we would put commands if the comparison
    ## is evaluated as false


    Write-Output -InputObject ('The percentage difference is {0}. Which is smaller than 10!' -f $diff)

    ## Move-Item -Path $file1 -Destination 'path/if/false'
}

最基本的解决方案是这样的:

$file1 = 100KB
$file2 = 110KB

if ($file2 -ge ($file1 * 1.1)) {
    "file2 is at least 10% bigger than File1"
}

我会使用如下所示的小型可重用辅助函数。

这将return两个文件大小之间的百分比差异(未四舍五入)作为正双数。

function Get-SizeDifference {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
        [string]$Path1,

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
        [string]$Path2
    )

    $size1 = (Get-Item -Path $Path1).Length
    $size2 = (Get-Item -Path $Path2).Length

    # avoid divide by zero
    if ($size1 -eq 0 -and $size2 -eq 0) { return 0 }
    if ($size1 -eq 0 -or $size2 -eq 0)  { return 100 }

    # you can get the size INCREASE from $size1 to $size2 using (could be a negative number)
    # ($size2 - $size1) / $size1 * 100

    # or the size DECREASE from $size1 to $size2 using (could be a negative number)
    # ($size2 - $size1) / $size2 * 100

    # however, to get the AVERAGE size difference you do (always a positive number)
    $difSize = [math]::Abs($size1 - $size2)
    $average = ($size1 + $size2) / 2
    $difSize * 100 / $average
}

然后像这样使用它:

if ((Get-SizeDifference 'C:\Intel\file1.sql' 'C:\Intel\file2.sql') -gt 10) {
    Write-Host "Percentage difference is more +/-10%"
}
else {
    write-host "Percentage difference is less than +/-10%"
}