如何找到映射驱动器的可用空间的百分比? [英] how to find the percentage of free space for mapped drives?

查看:198
本文介绍了如何找到映射驱动器的可用空间的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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?

推荐答案

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

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

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

>

This will output something like

FreeSpace
197890965504


b $ b

您可以通过添加 / format:value 开关,将它强制为单行:

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

> 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 for command can remove them for us when we do tokenizing:

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

现在可以使用存储在环境变量%FreeSpace%

中。

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

百分比现在有点棘手,因为批处理文件只支持32位整数进行计算。然而,你可能不需要计算这个字节;我认为兆字节是足够的:

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:

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

这应该工作,除非你的卷大于20 TiB。

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

这篇关于如何找到映射驱动器的可用空间的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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