如何根据文件夹的大小以 KB、MB 或 GB 为单位显示文件夹的大小?
How do I show the size of a folder in KB, MB, or GB depending on its size?
我有一个表单,可以在单击按钮时显示配置文件文件夹的大小。这是我为图片文件夹尝试过的几个代码变体...
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_KB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_MB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
Else { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
和
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_MB -ge 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
If ($Pictures_Size_MB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_KB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
我正在测试的图片文件夹是 5 MB,但它显示为 0.00 GB,我不明白为什么。在第一个代码示例中,如果我取出 If ($Pictures_Size_MB -gt 1024)
行,它会正确显示大小为 5.05 MB。我不确定出了什么问题,因为 5 小于 1024,所以它不应该显示 GB 数。
请注意这也需要在 Windows 7!
中工作
谢谢!
$Pictures_Size_MB
包含字符串 "5.05"
,它大于整数 1024
,这就是满足条件的原因。
我已经多次使用此代码:
# PowerShell Script to Display File Size
Function Format-DiskSize() {
[cmdletbinding()]
Param ([long]$Type)
If ($Type -ge 1TB) {[string]::Format("{0:0.00} TB", $Type / 1TB)}
ElseIf ($Type -ge 1GB) {[string]::Format("{0:0.00} GB", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0.00} MB", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0.00} KB", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0.00} Bytes", $Type)}
Else {""}
} # End of function
$BigNumber = "230993200055"
Format-DiskSize $BigNumber
来源:http://www.computerperformance.co.uk/powershell/powershell_function_format_disksize.htm
当您使用 -f
运算符时,您的输出(此处存储在 $Pictures_Size_MB
中)属于 System.String
类型,因此比较运算符不会像您预期的那样工作。
尝试先做数学,然后再格式化。像这样:
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum).sum
if ($Pictures_Size -gt 1TB) {
# Output as double
[System.Math]::Round(($Pictures_Size / 1TB), 2)
# Or output as string
"{0:N2} TB" -f ($Pictures_Size / 1TB)
}
您正在使用字符串格式化程序,因此将您的变量值存储为字符串。删除不必要的 "{0:N2} -f"
并使用 [Math]::Round()
我有一个表单,可以在单击按钮时显示配置文件文件夹的大小。这是我为图片文件夹尝试过的几个代码变体...
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_KB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_MB -gt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
Else { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
和
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum)
$Pictures_Size_KB = "{0:N2}" -f ($Pictures_Size.sum / 1KB)
$Pictures_Size_MB = "{0:N2}" -f ($Pictures_Size.sum / 1MB)
$Pictures_Size_GB = "{0:N2}" -f ($Pictures_Size.sum / 1GB)
If ($Pictures_Size_MB -ge 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_GB) GB" }
If ($Pictures_Size_MB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_MB) MB" }
If ($Pictures_Size_KB -lt 1024) { $Pictures_Box.Text = "Pictures - $($Pictures_Size_KB) KB" }
我正在测试的图片文件夹是 5 MB,但它显示为 0.00 GB,我不明白为什么。在第一个代码示例中,如果我取出 If ($Pictures_Size_MB -gt 1024)
行,它会正确显示大小为 5.05 MB。我不确定出了什么问题,因为 5 小于 1024,所以它不应该显示 GB 数。
请注意这也需要在 Windows 7!
中工作谢谢!
$Pictures_Size_MB
包含字符串 "5.05"
,它大于整数 1024
,这就是满足条件的原因。
我已经多次使用此代码:
# PowerShell Script to Display File Size
Function Format-DiskSize() {
[cmdletbinding()]
Param ([long]$Type)
If ($Type -ge 1TB) {[string]::Format("{0:0.00} TB", $Type / 1TB)}
ElseIf ($Type -ge 1GB) {[string]::Format("{0:0.00} GB", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0.00} MB", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0.00} KB", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0.00} Bytes", $Type)}
Else {""}
} # End of function
$BigNumber = "230993200055"
Format-DiskSize $BigNumber
来源:http://www.computerperformance.co.uk/powershell/powershell_function_format_disksize.htm
当您使用 -f
运算符时,您的输出(此处存储在 $Pictures_Size_MB
中)属于 System.String
类型,因此比较运算符不会像您预期的那样工作。
尝试先做数学,然后再格式化。像这样:
$Pictures_Size = (Get-ChildItem $User\Pictures -recurse | Measure-Object -property length -sum).sum
if ($Pictures_Size -gt 1TB) {
# Output as double
[System.Math]::Round(($Pictures_Size / 1TB), 2)
# Or output as string
"{0:N2} TB" -f ($Pictures_Size / 1TB)
}
您正在使用字符串格式化程序,因此将您的变量值存储为字符串。删除不必要的 "{0:N2} -f"
并使用 [Math]::Round()