问题描述
在Windows中,是否可以在不使用任何第三方工具的情况下从命令行获取文件夹大小?
Is it possible in Windows to get a folder's size from the command line without using any 3rd party tool?
我想要的结果与单击Windows资源管理器→属性中的文件夹。
I want the same result as you would get when right clicking the folder in the windows explorer → properties.
推荐答案
您可以递归地添加大小(以下是批处理文件) :
You can just add up sizes recursively (the following is a batch file):
@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes
然而,这有几个问题,因为 cmd
仅限于32位有符号整数运算。所以它会得到大于2 GiB的错误。此外,它可能会多次计数符号链接和结点,所以它最多是一个上限,而不是真正的大小(你会有这个问题与任何工具,虽然)。
However, this has several problems because cmd
is limited to 32-bit signed integer arithmetic. So it will get sizes above 2 GiB wrong. Furthermore it will likely count symlinks and junctions multiple times so it's at best an upper bound, not the true size (you'll have that problem with any tool, though).
另一个是PowerShell:
An alternative is PowerShell:
Get-ChildItem -Recurse | Measure-Object -Sum Length
或更短:
ls -r | measure -s Length
如果您希望它更漂亮:
switch((ls -r|measure -s Length).Sum) {
{$_ -gt 1GB} {
'{0:0.0} GiB' -f ($_/1GB)
break
}
{$_ -gt 1MB} {
'{0:0.0} MiB' -f ($_/1MB)
break
}
{$_ -gt 1KB} {
'{0:0.0} KiB' -f ($_/1KB)
break
}
default { "$_ bytes" }
}
您可以直接使用 cmd
:
powershell -noprofile -command "ls -r|measure -s Length"
我有一个部分完成的bignum库在批处理文件中,其中至少获得任意精度整数加法权。我真的应该释放它,我想: - )
I do have a partially-finished bignum library in batch files somewhere which at least gets arbitrary-precision integer addition right. I should really release it, I guess :-)
这篇关于从Windows命令行获取文件夹大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!