PowerShell中显示文件大小为KB,MB,GB或 [英] Powershell display file size as KB, MB, or GB

查看:2753
本文介绍了PowerShell中显示文件大小为KB,MB,GB或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,获取指定目录的文件大小的一个部分。

I have a section of a powershell script that gets the file size of a specified directory.

我能够得到测量到的变量不同单位的值,但我不知道显示相应的一个好方法。

I am able to get the values for different units of measurement into variables, but I don't know a good way to display the appropriate one.

$DirSize = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum)
$DirSizeKB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1KB)
$DirSizeMB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1MB)
$DirSizeGB = "{0:N2}" -f (($DirArray | Measure-Object -property length -sum).sum / 1GB)

如果字节数至少为1KB欲显示的KB值,如果KB的数目是至少有1 MB欲显示的MB等

If the number of bytes is at least 1KB I want the KB value displayed, If the number of KB is at least 1 MB I want MBs displayed and so on.

有谁知道做到这一点的好办法?

Does anyone know a good way to accomplish this?

感谢。

推荐答案

使用交换机或一组if语句。你的逻辑(伪code)应该是这样的:

Use a switch or a set of "if" statements. Your logic (pseudocode) should look like this:


  1. 是至少1GB大小?是的,在GB(否则...)显示

  2. 是大小至少为1MB?是的,在MB(否则...)显示

  3. 在KB显示。

请注意,您应该以相反的顺序从最大的尺寸最小的是测试。是的,我可以写在code你,但我怀疑你知道足以让到上述工作的脚本。这只是你曾经难倒了该方法。

Note that you should be testing in reverse order from the largest size to the smallest. Yes, I could have written the code for you but I suspect you know enough to turn the above into a working script. It's just the approach that had you stumped.

这篇关于PowerShell中显示文件大小为KB,MB,GB或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆