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 14:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] How to display the remaining space of a disk? DigestI View 19,795 Replies 46
Floor 16 Posted 2006-04-14 13:02 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by bagpipe at 2006-3-29 11:12:
The line "for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set freesize=%%a" doesn't work for me. I don't know why there's a ^ prefix. Please test it. I'm on Windows 2000 and it didn't work, so I used other methods to get the result...

Can you tell me what method you used to implement it? I happen to need to test a batch script under Windows 2000. Thanks.

[ Last edited by namejm on 2006-4-15 at 14:31 ]
Floor 17 Posted 2006-04-14 17:15 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
for /f "tokens=3" %%a in ('"dir /-c c:\ | find "Available bytes""') do set freesize=%%a

This method is very good!
Floor 18 Posted 2006-04-15 21:41 ·  中国 重庆 沙坪坝区 联通
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
Directly calling VBS should be much simpler
http://www.msfans.net/bbs/
Floor 19 Posted 2006-04-17 01:00 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Actually, batch (bat) is actually much simpler than VBScript (vbs).
Floor 20 Posted 2006-04-17 18:17 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by 3742668 at 2006-4-17 01:00:
Actually, bat is much simpler than vbs.

Then if you want to query the remaining space size of all existing disk partitions, requiring to use MB as the unit, without relying on third-party software, how to write the code? Can this function be implemented under 2000?
Floor 21 Posted 2006-04-21 19:43 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In the XP environment, I want to query the remaining space of each partition and display it in MB. Why can't the following code get the correct result?
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:\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "Available bytes"') do set freesize=%%b
set /a freesize=%freesize:~0,-3%/1049>nul
echo %% a drive remaining space:%freesize% MB
)
Floor 22 Posted 2006-04-21 22:44 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
It is estimated that it is because you have not enabled delayed environment variables, resulting in the value of freesize not being considered as changed. It is suggested to use SETLOCAL ENABLEDELAYEDEXPANSION to enable delayed environment variables, and additionally use!freesize! to replace %freesize%. But since it's XP, why consider a one-command solution?

@echo off
for /f "tokens=1,2" %%i in ('"wmic logicaldisk get freespace,caption"') do set %%i=%%j
echo The free space of drive C is: %c:% bytes
echo The free space of drive D is: %d:% bytes
echo The free space of drive E is: %e:% bytes
pause
Floor 23 Posted 2006-04-22 13:03 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Because I want to display the remaining space of all existing partitions and display it in MB, so I need to use the for statement. I don't understand the use of SETLOCAL ENABLEDELAYEDEXPANSION very well. Specifically, what parts need to be modified? Can I modify it based on the code I posted? Thank you.
Floor 24 Posted 2006-04-22 13:32 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Just now I took a look, your code obviously lacks a ), so first you should add a ) at the end. Secondly, there is an extra space between echo %% a. Thirdly, the algorithm is problematic and the result may be incorrect. Finally, the usage of SETLOCAL ENABLEDELAYEDEXPANSION:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
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:\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "Available bytes"') do set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul
echo %%a drive free space:!freesize! MB
)
)
pause

If you want to get the precise space number in MB, it's not impossible. As long as you change the division to subtraction and write your own code to calculate in two parts, of course, it's not very practical. In fact, it should be simpler to use wmic to save the remaining space numbers of each drive and then calculate the MB number through operations by yourself.
Floor 25 Posted 2006-04-22 13:44 ·  中国 福建 泉州 电信
高级用户
★★
论坛灌水专业户
Credits 613
Posts 266
Joined 2006-04-19 22:47
20-year member
UID 54113
From 河南省
Status Offline
Can't understand it too much~~~~~~~~~~
Floor 26 Posted 2006-04-22 17:16 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
to 3742668:
Thank you for your patient answer, which solved a long-standing problem for me. Thanks again.

"If you want to get the precise space number in megabytes, it's not impossible. As long as change the division to subtraction and write your own code in two parts to calculate, it's okay"
----Hehe, I didn't expect there is such a way of writing. Batch processing is really interesting. Can you post a code snippet to let me have a look?

"Of course, not very practical."
----Do you mean that the approach of dividing into two code segments is not practical or that displaying in megabytes is not practical? If it's the latter, I don't quite agree with you.

"Actually, using wmic can save the remaining space numbers of each disk, and then through calculation by yourself to get the megabyte number should be simpler."
----Will this generate temporary files? If temporary files are to be generated, it doesn't feel very perfect.

[ Last edited by namejm on 2006-4-22 at 17:42 ]
Floor 27 Posted 2006-04-22 18:40 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
1. It's relatively troublesome to calculate. Let me talk about the idea. You can try it yourself:
Actually, it's just like doing division by hand, but divided into two parts. First, use the first 7 digits to calculate with 1048576, then combine the remaining part after subtraction with the combination of digits other than the first 7 digits and calculate with 1048576, and finally combine the results of the two parts.
2. The reason why it's said to have no practicality is that considering the execution speed and complexity, it's better to use other methods.
3. I think you didn't look carefully at that batch processing of the wmic result. You can look again to see whether the result is saved in a variable or in a file.
Floor 28 Posted 2006-04-22 22:34 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by 3742668 at 2006-4-22 18:40:
3, I think you didn't look at that batch processing of the wmic result carefully. You can take another look to see whether the result is saved in a variable or in a file.

Hehe, it's not that I didn't look, but that I'm very unfamiliar with wmic and can't understand it. :(
Still, thank you for your explanation.
Floor 29 Posted 2006-04-22 22:47 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In the 2000 system environment, the statement `for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "可用字节"') do set freesize=%%b` always fails to get the correct result. How to modify the code?
Floor 30 Posted 2006-06-11 18:29 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Is there anyone who can answer the question on floor 29? I really want to know how to handle it.
Forum Jump: