windows CMD shell 中的可用空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/293780/
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
Free space in a CMD shell
提问by Nico
Is there a way to get the amount of free diskspace of a disk or a folder in a CMD without having to install some thirdparty applications?
有没有办法在无需安装某些第三方应用程序的情况下获取 CMD 中磁盘或文件夹的可用磁盘空间量?
I have a CMD that copies a big file to a given directory and could of course use the errorlevel return from the copy command, but then I have to wait for the time it takes to copy the file (eg...to that then the disk is full and the copy operation fails).
我有一个将大文件复制到给定目录的 CMD,当然可以使用从复制命令返回的错误级别,但是我必须等待复制文件所需的时间(例如...磁盘已满,复制操作失败)。
I would like to know before I start the copy if it is any idea at all. Tried the DU.EXE utility from Sysinternals, but that show occupied space only.
在我开始复制之前,我想知道它是否有任何想法。尝试了 Sysinternals 的 DU.EXE 实用程序,但仅显示占用的空间。
回答by Nico
If you run "dir c:\
", the last line will give you the free disk space.
如果您运行“ dir c:\
”,最后一行将为您提供可用磁盘空间。
Edit:Better solution: "fsutil volume diskfree c:
"
编辑:更好的解决方案:“ fsutil volume diskfree c:
”
回答by VonC
A possible solution:
一个可能的解决方案:
dir|find "bytes free"
a more "advanced solution", for Windows Xp and beyond:
更“高级的解决方案”,适用于 Windows Xp 及更高版本:
wmic /node:"%COMPUTERNAME%" LogicalDisk Where DriveType="3" Get DeviceID,FreeSpace|find /I "c:"
The Windows Management Instrumentation Command-line(WMIC) tool (Wmic.exe) can gather vast amounts of information about about a Windows Server 2003 as well as Windows XP or Vista. The tool accesses the underlying hardware by using Windows Management Instrumentation (WMI). Not for Windows 2000.
在Windows管理规范命令行(WMIC)工具(了Wmic.exe)可以收集大量的关于关于在Windows Server 2003以及Windows XP或Vista的信息。该工具使用 Windows Management Instrumentation (WMI) 访问底层硬件。不适用于 Windows 2000。
As noted by Alexander Stohrin the comments:
正如Alexander Stohr在评论中所指出的:
- WMIC can see policy based restrictions as well. (even if '
dir
' will still do the job), - '
dir
' is locale dependent.
- WMIC 也可以看到基于策略的限制。(即使 '
dir
' 仍然可以完成这项工作), - '
dir
' 取决于语言环境。
回答by Dewsri De Mel
Using this command you can find all partitions, size & free space: wmic logicaldisk get size, freespace, caption
使用此命令,您可以找到所有分区、大小和可用空间: wmic logicaldisk get size, freespace, caption
回答by lit
You can avoid the commas by using /-C on the DIR command.
您可以通过在 DIR 命令上使用 /-C 来避免使用逗号。
FOR /F "usebackq tokens=3" %%s IN (`DIR C:\ /-C /-O /W`) DO (
SET FREE_SPACE=%%s
)
ECHO FREE_SPACE is %FREE_SPACE%
If you want to compare the available space to the space needed, you could do something like the following. I specified the number with thousands separator, then removed them. It is difficult to grasp the number without commas. The SET /A is nice, but it stops working with large numbers.
如果要将可用空间与所需空间进行比较,可以执行以下操作。我用千位分隔符指定了数字,然后删除了它们。没有逗号很难掌握数字。SET /A 很好,但它停止处理大量数字。
SET EXITCODE=0
SET NEEDED=100,000,000
SET NEEDED=%NEEDED:,=%
IF %FREE_SPACE% LSS %NEEDED% (
ECHO Not enough.
SET EXITCODE=1
)
EXIT /B %EXITCODE%
回答by paxdiablo
The following script will give you free bytes on the drive:
以下脚本将为您提供驱动器上的可用字节:
@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=3" %%a in ('dir c:\') do (
set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo %bytesfree%
endlocal && set bytesfree=%bytesfree%
Note that this depends on the output of your dir
command, which needs the last line containing the free space of the format 24 Dir(s) 34,071,691,264 bytes free
. Specifically:
请注意,这取决于您的dir
命令的输出,它需要最后一行包含 format 的可用空间24 Dir(s) 34,071,691,264 bytes free
。具体来说:
- it must be the last line (or you can modify the
for
loop to detect the line explicitly rather than relying on settingbytesfree
for every line). - the free space must be the third "word" (or you can change the
tokens=
bit to get a different word). - thousands separators are the
,
character (or you can change the substitution from comma to something else).
- 它必须是最后一行(或者您可以修改
for
循环以明确检测该行,而不是依赖于bytesfree
每一行的设置)。 - 可用空间必须是第三个“单词”(或者您可以更改
tokens=
位以获得不同的单词)。 - 千位分隔符是
,
字符(或者您可以将替换从逗号更改为其他内容)。
It doesn't pollute your environment namespace, setting only the bytesfree
variable on exit. If your dir
output is different (eg, different locale or language settings), you will need to adjust the script.
它不会污染您的环境命名空间,仅bytesfree
在退出时设置变量。如果您的dir
输出不同(例如,不同的区域设置或语言设置),您将需要调整脚本。
回答by tommym
df.exe
文件
Shows all your disks; total, used and free capacity. You can alter the output by various command-line options.
显示所有磁盘;总容量、已用容量和可用容量。您可以通过各种命令行选项更改输出。
You can get it from http://www.paulsadowski.com/WSH/cmdprogs.htm, http://unxutils.sourceforge.net/or somewhere else. It's a standard unix-util like du.
你可以从它http://www.paulsadowski.com/WSH/cmdprogs.htm,http://unxutils.sourceforge.net/或其他地方。它是一个像 du 一样的标准 unix-util。
df -h
will show all your drive's used and available disk space. For example:
df -h
将显示您驱动器的所有已用和可用磁盘空间。例如:
M:\>df -h
Filesystem Size Used Avail Use% Mounted on
C:/cygwin/bin 932G 78G 855G 9% /usr/bin
C:/cygwin/lib 932G 78G 855G 9% /usr/lib
C:/cygwin 932G 78G 855G 9% /
C: 932G 78G 855G 9% /cygdrive/c
E: 1.9T 1.3T 621G 67% /cygdrive/e
F: 1.9T 201G 1.7T 11% /cygdrive/f
H: 1.5T 524G 938G 36% /cygdrive/h
M: 1.5T 524G 938G 36% /cygdrive/m
P: 98G 67G 31G 69% /cygdrive/p
R: 98G 14G 84G 15% /cygdrive/r
Cygwin is available for free from: https://www.cygwin.com/It adds many powerful tools to the command prompt. To get just the available space on drive M (as mapped in windows to a shared drive), one could enter in:
Cygwin 可从以下网址免费获得:https: //www.cygwin.com/它为命令提示符添加了许多强大的工具。要仅获取驱动器 M 上的可用空间(在 Windows 中映射到共享驱动器),可以输入:
M:\>df -h | grep M: | awk '{print }'
回答by Zsolt Hidasi
Using paxdiabloexcellent solution I wrote a little bit more sophisticated batch script, which uses drive letter as the incoming argument and checks if drive exists on a tricky (but not beauty) way:
使用paxdiablo优秀的解决方案,我编写了一个更复杂的批处理脚本,它使用驱动器号作为传入参数并检查驱动器是否以一种棘手(但不是美观)的方式存在:
@echo off
setlocal enableextensions enabledelayedexpansion
set chkfile=drivechk.tmp
if "%1" == "" goto :usage
set drive=%1
set drive=%drive:\=%
set drive=%drive::=%
dir %drive%:>nul 2>%chkfile%
for %%? in (%chkfile%) do (
set chksize=%%~z?
)
if %chksize% neq 0 (
more %chkfile%
del %chkfile%
goto :eof
)
del %chkfile%
for /f "tokens=3" %%a in ('dir %drive%:\') do (
set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo %bytesfree% byte(s) free on volume %drive%:
endlocal
goto :eof
:usage
echo.
echo usage: freedisk ^<driveletter^> (eg.: freedisk c)
note1: you may type simple letter (eg. x) or may use x: or x:\ format as drive letter in the argument
注意 1:您可以输入简单的字母(例如 x),也可以使用 x: 或 x:\ 格式作为参数中的驱动器号
note2: script will display stderr from %chkfile% only if the size bigger than 0
注意2:仅当大小大于 0 时,脚本才会显示来自 %chkfile% 的 stderr
note3: I saved this script as freedisk.cmd (see usage)
注意3:我将此脚本保存为 freedisk.cmd(见用法)
回答by Pedro Robson Le?o
I make a variation to generate this out from script:
我做了一个变化来从脚本中生成它:
volume C: - 49 GB total space / 29512314880 byte(s) free
volume C: - 49 GB total space / 29512314880 byte(s) free
I use diskpartto get this information.
我使用diskpart来获取这些信息。
@echo off
setlocal enableextensions enabledelayedexpansion
set chkfile=drivechk.tmp
if "%1" == "" goto :usage
set drive=%1
set drive=%drive:\=%
set drive=%drive::=%
dir %drive%:>nul 2>%chkfile%
for %%? in (%chkfile%) do (
set chksize=%%~z?
)
if %chksize% neq 0 (
more %chkfile%
del %chkfile%
goto :eof
)
del %chkfile%
echo list volume | diskpart | find /I " %drive% " >%chkfile%
for /f "tokens=6" %%a in ('type %chkfile%' ) do (
set dsksz=%%a
)
for /f "tokens=7" %%a in ('type %chkfile%' ) do (
set dskunit=%%a
)
del %chkfile%
for /f "tokens=3" %%a in ('dir %drive%:\') do (
set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo volume %drive%: - %dsksz% %dskunit% total space / %bytesfree% byte(s) free
endlocal
goto :eof
:usage
echo.
echo usage: freedisk ^<driveletter^> (eg.: freedisk c)
回答by gimel
Is cscript a 3rd party app? I suggest trying Microsoft Scripting, where you can use a programming language (JScript, VBS) to check on things like List Available Disk Space.
cscript 是第 3 方应用程序吗?我建议尝试Microsoft Scripting,您可以在其中使用编程语言(JScript、VBS)来检查List Available Disk Space 之类的内容。
The scripting infrastructure is present on all current Windows versions (including 2008).
脚本基础结构存在于所有当前的 Windows 版本(包括 2008)上。