China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 13:16
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] How to display the remaining space of a disk? DigestI View 19,787 Replies 46
Original Poster Posted 2006-03-26 00:19 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The operating system is WinXP. I want to realize the function of displaying the remaining disk space without using third-party software. I've been lurking for several days and wrote the following code. Please help me.
@echo off
rem There are problems
rem
rem 1、How to convert the number of bytes into a number in MB unit?
rem
rem 2、How to not generate temporary files?
color 1f
title Display remaining space on drive C
del size.txt>nul 2>nul
dir /-c c:\|find "Available bytes">size.txt
for /f "tokens=3" %%a in (size.txt) do echo There is %%a bytes of remaining space on drive C
del size.txt>nul 2>nul
echo.
echo.
echo.
echo Press any key to exit
pause>nul

  Now a perfect solution has been found. Please see the code:

@echo off
setlocal enabledelayedexpansion

:Main
cls
set /a num2=1024*1024
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%i:\ (
set num3_str=
set num4=
for /f "tokens=3" %%j in ('dir /-c %%i:\') do set num1=%%j
if not "!num1!"=="0" (
call :loop
for /f "delims=0 tokens=*" %%k in ("!num3_str!") do set num3_str=%%k
echo.
echo The remaining space on drive %%i is !num3_str! MB
echo.
)
)
)
)
pause
goto Main

:loop
:: Find quotient
set /a num3=%num4%%num1:~0,1%/%num2%
:: Quotient sequence
set num3_str=%num3_str%%num3%
:: Find remainder
set /a num4=%num4%%num1:~0,1%%%%num2%
if %num4% equ 0 set num4=
set num1=%num1:~1%
if not "%num1%"=="" goto loop
goto :eof

  Source: Find the exact value of the remaining space of all partitions (in MB unit)

[ Last edited by namejm on 2007-3-11 at 02:26 PM ]
Floor 2 Posted 2006-03-26 01:08 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Use:

set /a freesize=%%a/1048576

To calculate MB, freesize will hold the number in MB units.

The method without generating temporary files may be a bit crude. Try this:
for /f "tokens=3" %%a in ('dir /-c c:\') do set /a freesize=%%a/1048576

Since the line you need is in the last line, although this seems a bit tedious, it does meet the need.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2006-03-26 12:06 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
@echo off
echo Drive Letter Free Space Partition Size
wmic logicaldisk where caption="c:" get size,freespace,caption | findstr /v /i "caption"
pause>nul
Floor 4 Posted 2006-03-26 14:27 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
What is wmic?
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 5 Posted 2006-03-26 15:56 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
试了一下,提示说找不到操作数,不知何故.

(Tried it, and it said "operand not found", don't know why.)
Floor 6 Posted 2006-03-26 20:12 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
### Re Climbing:


The WMI Command Line provides a simple command-line interface to the Windows Management Instrumentation (WMI). WMIC offers a simple interface to WMI, enabling management of computers running Microsoft Windows using WMI. WMIC interoperates with existing command-line programs and utility commands, and is easily extensible via scripts or other management-oriented applications.

WMIC allows the following operations:

Typically use "aliases" that make WMI more intuitive to browse WMI schemas and query their classes and instances.
Use local computers, remote computers, or multiple computers in a single command.
Customize aliases and output in the desired format.
Create and execute WMIC-based scripts.

WMI providers allow WMI to manage a wide range of hardware components, operating system subsystems, and application systems. WMIC can be used with all schemas executed by these WMI providers.

WMIC can be used to remotely manage any computer with WMI on any computer where WMIC is enabled. For WMIC, there is no need for it to be available on the remotely managed computer in order to manage that remotely managed computer.

Scenarios
The following typical scenarios make tasks easy to perform using WMIC:
Local management of a computer—Use WMIC commands to manage a local computer.
Remote management of a computer—Use WMIC to manage another computer.
Remote management of multiple computers—Use WMIC on one computer to manage multiple computers in a single command.
Remote management of a computer (using a remote session)—Use remote session technologies such as Telnet or Terminal Services to connect to a remote computer and manage it using WMIC.
Automated management using management scripts—Use WMIC to write simple management scripts to automate management of computers (local, remote, or multiple computers—sequentially or simultaneously).
Aliases
When using WMIC through an intermediate helper called an "alias," the WMI structure is accessible. Aliases are used to capture characteristics of WMI levels associated with some specific tasks (such as disk or network management). Aliases can be used to provide better names for WMI classes, properties, and methods, or to arrange properties in a useful output format. The output format can include specific property values or be formatted in a way appropriate for a particular representation policy or function. For example, an alias might have a "BRIEF" format that lists only the basic property values that can identify a visualized object via the alias. Retrieve management data in XML format, and that data is processed via built-in or custom XSL output formats.

See Also

WMI Overview
Calling WMIC
Using WMIC
Extending Aliases
References
Managing Systems with WMIC
Windows Management Instrumentation Command-line (WMIC) Tool

The above is quoted from the XP Help and Support Center document
ms-its:C:\WINDOWS\Help\WMIC.chm::/WMIC_Overview.htm

### Re namejm:

Need to use find to filter out text lines that are not file sizes

for /f "tokens=3" %%a in ('dir /-c c:\^|find "Available bytes"') do set /a freesize=%%a/1048576
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2006-03-26 20:34 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Hmm, I’ve learned something.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2006-03-27 02:08 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
to willsort:
You need to use find to filter out text lines that are not file sizes

for /f "tokens=3" %%a in ('dir /-c c:\^|find "Available bytes"') do set /a freesize=%%a/1048576

I tried running this code and used echo %freesize% to display the result, but it turned out to be a negative number, which was quite surprising. For example, my C drive is 2305712128 bytes, but the result showed -1897. Could it be that the CMD has gone crazy?
Floor 9 Posted 2006-03-27 09:14 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

CMD is not deranged, it's because the integer processed by set is too large. set stores integers in two bytes, with a 32-bit storage range limit, that is, its processing range is 2^-31~2^31-1, and your disk space exceeds this range and overflows.

I don't have a very good solution for this, only an approximate algorithm of discarding the last three digits and then dividing by 1049.

for /f "tokens=3" %%a in ('dir /-c c:\^|find "Available bytes"') do set freesize=%%a
set /a freesize=%freesize:~0,-3%/1049>nul
echo Freesize:%freesize%
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2006-03-27 17:58 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Thanks to the moderator's detailed explanation. It seems I can only use a somewhat approximate number.
Floor 11 Posted 2006-03-29 11:12 ·  中国 北京 顺义区 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
The `for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set freesize=%%a` This sentence I can't execute, I don't know why I added the ^ leading character, please everyone test it. Mine is 2000, it didn't execute successfully, so I switched to other methods to get the result.........
Floor 12 Posted 2006-03-29 11:24 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
XP, SP2 is normal.
Try for /f "tokens=3" %%a in ('"dir /-c c:\ | find "Available bytes""') do set freesize=%%a
Use ^ to escape, otherwise it will execute dir /-c c:\ find "Available bytes"
Floor 13 Posted 2006-03-29 11:41 ·  中国 广西 贺州 电信
初级用户
Credits 58
Posts 17
Joined 2006-03-22 10:05
20-year member
UID 52567
Status Offline
Can you detect the remaining disk space under DOS?
Floor 14 Posted 2006-04-13 23:01 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Why can't the following method get the correct result when you want to query the remaining space size of all existing disk partitions?

for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do @(
if exist %%a:\recycler\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "可用字节"') do set freesize=%%b
set /a freesize=%freesize:~0,-3%/1049>nul
echo %% a 盘剩余空间:%freesize% MB
)

[ Last edited by namejm on 2006-4-14 at 12:59 ]
Floor 15 Posted 2006-04-14 00:13 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Originally posted by MIN at 2006-3-29 11:41:
Can DOS detect the remaining disk space?

Yes. See: How to detect the free space of a partition
Forum Jump: