|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『楼 主』:
[已结]如何显示磁盘的剩余空间?
使用 LLM 解释/回答一下
操作系统为WinXP,想不借助第三方软件实现显示磁盘剩余空间的功能,潜水了好几天,写了如下代码,请大家帮帮我.
@echo off
rem 存在问题
rem
rem 1、怎样把字节数转换为以M为单位的数?
rem
rem 2、怎样才能不产生临时文件?
color 1f
title 显示C盘剩余空间
del size.txt>nul 2>nul
dir /-c c:\|find "可用字节">size.txt
for /f "tokens=3" %%a in (size.txt) do echo C盘还有 %%a 字节剩余空间
del size.txt>nul 2>nul
echo.
echo.
echo.
echo 按任意键退出
pause>nul
目前已经找到了完美的解决方案,请看代码:
@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 %%i 盘剩余空间为 !num3_str! MB
echo.
)
)
)
)
pause
goto Main
:loop
:: 求商
set /a num3=%num4%%num1:~0,1%/%num2%
:: 求商序列
set num3_str=%num3_str%%num3%
:: 求余
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
出处: 求所有分区剩余空间的精确值(以MB为单位)
Last edited by namejm on 2007-3-11 at 02:26 PM ]
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 ]
|
|
2006-3-26 00:19 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
用:
set /a freesize=%%a/1048576
来计算MB,freesize成保存的就是MB为单位的数字了。
不产生临时文件的方法可能有点不雅观,试试这样:
for /f "tokens=3" %%a in ('dir /-c c:\') do set /a freesize=%%a/1048576
由于你需要的行在最后一行,虽然这样显得有点啰索,但确实能够满足需要。
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.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-3-26 01:08 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@echo off
echo 盘符 剩余空间 分区大小
wmic logicaldisk where caption="c:" get size,freespace,caption | findstr /v /i "caption"
pause>nul
@echo off
echo Drive Letter Free Space Partition Size
wmic logicaldisk where caption="c:" get size,freespace,caption | findstr /v /i "caption"
pause>nul
|
|
2006-3-26 12:06 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
wmic是什么东东?
What is wmic?
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2006-3-26 14:27 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by Climbing at 2006-3-26 01:08:
用:
set /a freesize=%%a/1048576
来计算MB,freesize成保存的就是MB为单位的数字了。
不产生临时文件的方法可能有点不雅观,试试这样:
for /f "to ...
试了一下,提示说找不到操作数,不知何故.
试了一下,提示说找不到操作数,不知何故.
(Tried it, and it said "operand not found", don't know why.)
|
|
2006-3-26 15:56 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Re Climbing:
WMI 命令行WMI 命令行提供了到 Windows 管理规范 (WMI) 的简单命令行接口。WMIC 提供了到 WMI 的简单接口,这样即可利用 WMI 管理运行 Microsoft Windows 的计算机。WMIC 与现有命令行程序和实用程序命令相互操作,且很容易通过脚本或其它面向管理的应用程序来扩展 WMIC。
WMIC 允许以下操作:
通常使用使 WMI 更直观的“别名”,浏览 WMI 模式并查询它们的类和实例。
以单一命令方式使用本地计算机、远程计算机或多台计算机。
自定义别名并输出符合需要的格式。
创建和执行基于 WMIC 的脚本。
WMI 提供程序允许 WMI 管理大量的硬件组件、操作系统子系统和应用程序系统。WMIC 可与由这些 WMI 提供程序执行的所有模式一起使用。
可以在任何启用了 WMIC 的计算机上,使用 WMIC 远程管理任何具有 WMI 的计算机。对 WMIC 而言,不必为了管理远程管理的计算机而使其在该远程管理的计算机上可用。
方案
下列典型方案,使用 WMIC 使任务执行容易:
本地管理计算机—使用 WMIC 命令管理本地计算机。
远程管理计算机—使用 WMIC 管理其它计算机。
远程管理多台计算机—在一台计算机上通过 WMIC 使用单一命令管理多台计算机。
远程管理计算机(使用远程会话)—使用远程会话技术(如 Telnet 或终端服务)连接到远程计算机并使用 WMIC 对其进行管理。
使用管理脚本进行自动管理—使用 WMIC 编写简单的管理脚本,自动化管理计算机(本地、远程或多台计算机—顺序进行或同时进行)。
别名
通过称为“别名”的中间援助器使用 WMIC 时,可访问 WMI 结构。别名用来捕捉和一些特定的任务(比如磁盘或网络管理)相关联的 WMI 等级的特征。别名可用于为 WMI 类别、属性和方法提供更好的名称,或者以有用的输出格式排列属性。输出格式可以包括具体的属性值,或者以对某个具体表示法策略或功能适当的方式进行格式化。例如,别名可能具有“BRIEF”格式,此格式仅列出可通过别名来鉴定可视化对象的基本属性值。以 XML 格式检索管理数据,而且该数据通过内置或自定义 XSL 输出格式进行处理。
请参阅
WMI 概述
调用 WMIC
使用 WMIC
扩展别名
参考
用 WMIC 管理系统
Windows 管理规范命令行 (WMIC) 工具
以上引自XP帮助与支持中心文档
ms-its:C:\WINDOWS\Help\WMIC.chm::/WMIC_Overview.htm
Re namejm:
需要使用find过滤掉非文件大小的文本行
for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set /a freesize=%%a/1048576
### 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-3-26 20:12 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
|
2006-3-26 20:34 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
to willsort:
需要使用find过滤掉非文件大小的文本行
for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set /a freesize=%%a/1048576
试运行了这段代码,用echo %freesize%来显示结果,结果居然显示负数,让人大跌眼镜.比如,我的C盘大小为2305712128字节,结果显示的是-1897.难道CMD神经错乱了不成?
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?
|
|
2006-3-27 02:08 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re namejm:
CMD没有神经错乱,是set处理的整数太大了,set使用双字节存储整数,有32位的存贮范围限制,也就是说它的处理范围是2^-31~2^31-1,你的磁盘空间超过了这个范围溢出了。
对此我没有太好的解决办法,只有舍弃后三位后除以1049的近似算法。
for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set freesize=%%a
set /a freesize=%freesize:~0,-3%/1049>nul
echo Freesize:%freesize%
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-3-27 09:14 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
谢谢版主的详细解答.看来只能用个差不多的数字了.
Thanks to the moderator's detailed explanation. It seems I can only use a somewhat approximate number.
|
|
2006-3-27 17:58 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set freesize=%%a这句我执行不了,不知道为什么加个^前导字符,请各位试验下,我的是2000,没有执行成功,所以改用其他方法得到结果.........
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.........
|
|
2006-3-29 11:12 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
XP,SP2正常。
试试for /f "tokens=3" %%a in ('"dir /-c c:\ | find "可用字节""') do set freesize=%%a
用^来转义,否则执行的是 dir /-c c:\ find "可用字节"
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"
|
|
2006-3-29 11:24 |
|
|
MIN
初级用户
 
积分 58
发帖 17
注册 2006-3-22
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
DOS下能不能检测磁盘剩余空间呢?
Can you detect the remaining disk space under DOS?
|
|
2006-3-29 11:41 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
如果想查询所有存在的磁盘分区的剩余空间大小,用下面的方法为什么得不到正确结果呢
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 ]
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 ]
|
|
2006-4-13 23:01 |
|
|
DOSforever
金牌会员
     
积分 4639
发帖 2239
注册 2005-1-30
状态 离线
|
|
2006-4-14 00:13 |
|