Windows 批处理文件获取 C:\ 驱动器总空间和可用空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17255925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch file to get C:\ drive total space and free space available
提问by Mani
I need a bat file to get C:\ drive total space and free space available in GB (giga bytes) in a Windows system and create a text file with the details.
我需要一个 bat 文件来获取 Windows 系统中 C:\ 驱动器的总空间和可用空间(以 GB(千兆字节)为单位),并创建一个包含详细信息的文本文件。
Note: i dont want to use any external utilities.
注意:我不想使用任何外部实用程序。
回答by Endoro
cut 9 digits of the size by bytes to get the size in GB:
按字节切割大小的 9 位数字以获得以 GB 为单位的大小:
@echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
SET "diskfree=!disktotal!"
SET "disktotal=!diskavail!"
SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
(ECHO(Information for volume %volume%
ECHO(total %disktotal:~0,-9% GB
ECHO(avail. %diskavail:~0,-9% GB)>size.txt
TYPE size.txt
cmd can calculate only with numbers up to 2^31-1
??(2,147,483,647 ~ 2.000001 Gigabytes)
cmd 只能计算最大2^31-1
??(2,147,483,647 ~ 2.000001 GB) 的数字
回答by Joe Bourne
Not a complete solution by any means, but someone might find this helpful:
无论如何都不是一个完整的解决方案,但有人可能会发现这有帮助:
dir | find "bytes"
回答by mojo
This is probably not at all what you want since it uses PowerShell, but "external utilities" is a bit nebulous and leaves me some wiggle room. Plus, it's essentially a one-liner.
这可能根本不是你想要的,因为它使用 PowerShell,但“外部实用程序”有点模糊,给我留下了一些回旋余地。另外,它本质上是一个单线。
SETLOCAL
FOR /F "usebackq tokens=1,2" %%f IN (`PowerShell -NoProfile -EncodedCommand "CgBnAHcAbQBpACAAVwBpAG4AMwAyAF8ATABvAGcAaQBjAGEAbABEAGkAcwBrACAALQBGAGkAbAB0AGUAcgAgACIAQwBhAHAAdABpAG8AbgA9ACcAQwA6ACcAIgB8ACUAewAkAGcAPQAxADAANwAzADcANAAxADgAMgA0ADsAWwBpAG4AdABdACQAZgA9ACgAJABfAC4ARgByAGUAZQBTAHAAYQBjAGUALwAkAGcAKQA7AFsAaQBuAHQAXQAkAHQAPQAoACQAXwAuAFMAaQB6AGUALwAkAGcAKQA7AFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAoACQAdAAtACQAZgApACwAJABmAH0ACgA="`) DO ((SET U=%%f)&(SET F=%%g))
@ECHO Used: %U%
@ECHO Free: %F%
Since batch/CMD is bad at nearly everything, I decided to use PowerShell, which is meant for such stuff and has quick and easy access to WMI.
由于批处理/CMD 几乎在所有方面都很糟糕,因此我决定使用 PowerShell,它适用于此类内容并且可以快速轻松地访问 WMI。
Here's the PowerShell code:
这是 PowerShell 代码:
Get-WMIObject -Query "SELECT * FROM Win32_LogicalDisk WHERE Caption='C:'" `
| % {
$f = [System.Math]::Round($_.FreeSpace/1024/1024/1024,1);
$t = [System.Math]::Round($_.Size/1024/1024/1024,1);
Write-Host ('' + ($t-$f) + ',' + $f);
}
This spits out the two values separated by a comma. Now, if only we could do this in a FOR loop!
这会吐出由逗号分隔的两个值。现在,如果我们能在 FOR 循环中做到这一点就好了!
PowerShell has the nice ability to accept a Base64-encoded command (to eliminate the need for escaping and making the code hard to read), so all we need to do is shrink this command as much as possible (to reduce the size of the encoded string—strictly a nicety, not absolutely necessary). I also reduced the sizes to integers, which rounded them. It's at least closer than discarding the lower-order decimal digits.
PowerShell 有很好的能力接受 Base64 编码的命令(消除转义的需要和使代码难以阅读),所以我们需要做的就是尽可能地缩小这个命令(以减少编码的大小)字符串 - 严格来说是一个细节,不是绝对必要的)。我还将大小减少到整数,将它们四舍五入。它至少比丢弃低位十进制数字更接近。
Shrinking the encoded command and encoding it in PowerShell looks like this:
缩小编码的命令并在 PowerShell 中对其进行编码如下所示:
$code = {
gwmi Win32_LogicalDisk -Filter "Caption='C:'"|%{$g=1073741824;[int]$f=($_.FreeSpace/$g);[int]$t=($_.Size/$g);Write-Host ($t-$f),$f}
}
$enc = [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
Write-Host $enc
(See PowerShell /? for more details.)
(有关详细信息,请参阅 PowerShell /?。)
I would expect this to run on any Win7 or Win8 machine in existence. The PoSH code doesn't rely on any advanced features (except maybe the EncodedCommand bit), so if PoSH is installed on the XP or Vista machine, there's a good chance of it working. I can't speak about the history of MS pushing PoSH via Windows Update, but I think there's a good chance that this will work ubiquitously.
我希望它可以在现有的任何 Win7 或 Win8 机器上运行。PoSH 代码不依赖于任何高级功能(可能除了 EncodedCommand 位),所以如果 PoSH 安装在 XP 或 Vista 机器上,它很有可能工作。我不能谈论 MS 通过 Windows 更新推动 PoSH 的历史,但我认为这很有可能无处不在。
回答by scriptmastere02
This should work in batch:
这应该批量工作:
for /f "tokens=2" %%S in ('wmic volume get DriveLetter^, FreeSpace ^| findstr "^C:"') do set space=%%S
echo %space%