windows 如何找到映射驱动器的可用空间百分比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2539262/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:13:30  来源:igfitidea点击:

how to find the percentage of free space for mapped drives?

windowscommand-linescripting

提问by Arunachalam

Is it possible to programatically find the free space available in mapped drives?

是否可以通过编程方式找到映射驱动器中的可用空间?

How to find the percentage of free space in your drive using ms-dos.
It may be easy to find the free space for a drive in ur hard disc but i need to find the free-space of mapped drives.

如何使用 ms-dos 查找驱动器中可用空间的百分比。
在您的硬盘中找到驱动器的可用空间可能很容易,但我需要找到映射驱动器的可用空间。

I have mapped some file servers in my systems.

我已经在我的系统中映射了一些文件服务器。

It is possible to see this in My Computer, but how do show it in a command prompt?

可以在“我的电脑”中看到它,但是如何在命令提示符中显示它?

回答by Joey

(Taken from an old answer of mine over at Server Fault)

(取自我在 Server Fault 上的旧答案)

The easiest way to reliably get at the free disk space is using WMI. When trying to parse the output of diryou get all kinds of funny problems, at the very least with versions of Windows in other languages. You can use wmicto query the free space on a drive:

可靠地获得可用磁盘空间的最简单方法是使用 WMI。尝试解析输出时dir会遇到各种有趣的问题,至少对于其他语言的 Windows 版本。您可以使用wmic查询驱动器上的可用空间:

wmic logicaldisk where "DeviceID='C:'" get FreeSpace

This will output something like

这将输出类似

FreeSpace
197890965504

You can force this into a single line by adding the /format:valueswitch:

您可以通过添加/format:value开关将其强制为一行:

> wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value

FreeSpace=197890965504

There are a few empty lines there, though (around three or four) which don't lend themselves well for processing. Luckily the forcommand can remove them for us when we do tokenizing:

那里有一些空行(大约三四个),它们不适合处理。幸运的是,for当我们进行标记化时,该命令可以为我们删除它们:

for /f "usebackq delims== tokens=2" %x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%x

The nice thing here is that since we're only using the second token all empty lines (that don't have a second token) get ignored.

这里的好处是,因为我们只使用第二个标记,所以所有空行(没有第二个标记)都会被忽略。

Remember to double the %signs when using this in a batch file:

%在批处理文件中使用它时,请记住将符号加倍:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x

You can now use the free space which is stored in the environment variable %FreeSpace%.

您现在可以使用存储在环境变量中的可用空间%FreeSpace%



Getting percentages now is a little tricky since batch files only support 32-bit integers for calculation. However, you probably don't need to calculate this to the byte; I think megabytes are quite enough:

现在获取百分比有点棘手,因为批处理文件仅支持 32 位整数进行计算。但是,您可能不需要将其计算为字节;我认为兆字节就足够了:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get Size /format:value`) do set Size=%%x
set FreeMB=%FreeSpace:~0,-6%
set SizeMB=%Size:~0,-6%
set /a Percentage=100 * FreeMB / SizeMB
echo C: is %Percentage% % free

This should work unless your volumes get larger than 20 TiB.

除非您的卷大于 20 TiB,否则这应该有效。

回答by Stephen Kellett

You need GetDiskFreeSpaceEx. Works with drives, mapped drives, etc.

你需要GetDiskFreeSpaceEx。适用于驱动器、映射驱动器等。

ULARGE_INTEGER free;
ULARGE_INTEGER total;
ULARGE_INTEGER totalFree;
BOOL           ok;

ok = GetDiskSpaceFreeEx(path, &free, &total, &totalFree);
if (ok)
{
// do your sums here, then printf the result
}

回答by FoxDeploy

You can do this in modern Windows very easily using PowerShell.

您可以使用 PowerShell 在现代 Windows 中轻松完成此操作。

This cmdlet will return a nice table containing your drives and some info about their space.

此 cmdlet 将返回一个漂亮的表格,其中包含您的驱动器和有关其空间的一些信息。

get-psdrive | Where Free*

Name           Used (GB)     Free (GB) Provider      Root                                                                  CurrentLocation
----           ---------     --------- --------      ----                                                                  ---------------
C                 101.65         65.59 FileSystem    C:\                                                                  WINDOWS\system32
D                 801.55        129.96 FileSystem    D:\
R                 443.17       2351.22 FileSystem    R:\
X                 119.28        104.29 FileSystem    X:\

Probably the easiest and shortest way to get what you're looking for. Remember to launch PowerShell, not cmd.exe, to run these cmds.

可能是获得所需内容的最简单、最短的方法。请记住启动 PowerShell 而不是 cmd.exe 来运行这些 cmd。

get-psdrive | Where Free*

获取-psdrive | 哪里免费*