中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-11 10:48
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Need a batch file to get the remaining free space on a hard drive.
Printable Version  861 / 8
Floor1 binjie Posted 2009-02-23 17:03
新手上路 Posts 9 Credits 17
Need a batch file to get the remaining free space on a hard drive.


First get the size of drive A. If it is less than 20M then execute the del command; if it is greater than 20M then skip executing del.


Thanks, experts!
Floor2 zhengqian529 Posted 2009-02-23 17:35
初级用户 Posts 93 Credits 100 From 湖南
del is the command for deleting files, rd is the command for deleting folders. I don't dare try your problem. But I have some code reposted from the Internet that can check the machine's disk free space.
Floor3 binjie Posted 2009-02-23 17:49
新手上路 Posts 9 Credits 17
The one above doesn't work when tested,


This one works, but I don't understand how to add a size check and then execute a command afterward.

@echo off&color b
set disk=x
for /f %%a in ('wmic logicaldisk where "caption='%disk%:'" get FreeSpace /value 2^>nul') do set %%a
if %FreeSpace% LSS 1024 set/a FreeSpace=(FreeSpace*1000)/1024&echo.&echo.&echo.&echo. %disk% drive free space is !Free! KB&pause>nul&goto :eof
set mn=MB
call :lp 1048576
set mn=GB
set/a modr=0
set FreeSpace=%t%
set t=0
call :lp 1024
goto :eof
:lp
set Free=%FreeSpace:~0,1%
set/a mod=modr*10+Free
set/a r=mod/%1
set t=%t%%r%
if "%t%"=="0" set t=
set/a modr=mod%%%1
set FreeSpace=%FreeSpace:~1%
if defined FreeSpace goto lp
set/a w=0
if %t% gtr 1024 goto :eof
set t=%t%.
set n=2
:loop
set /a modr*=10,r=modr/%1,w+=1
if %w% geq %n% set/a modr=modr%%%1*10/%1 && goto lpok
set t=%t%%r%
set/a modr=modr%%%1
goto loop
:lpok
cls
for /f "delims=0 tokens=*" %%i in ("%t%%r% %mn%") do set t=%%i
echo.&echo.&echo. %disk% drive free space is: %t%&echo.&pause&exit
Floor4 zhengqian529 Posted 2009-02-23 18:01
初级用户 Posts 93 Credits 100 From 湖南
Are you thinking of deleting all files on drive A, or just deleting files?
1. Checking the size: look at the last line of your program, %t% records the drive size. You can use an if statement. Convert 20M into the same unit as %t%, such as MB, KB, or byte.
2. Deleting files: after comparing, execute the del or rd command, for example:
if %t% lss 20 del /s %disk%:\ (unit is MB, deletes all files under this drive; if you want to delete all files underneath, you can use rd /s %disk%:\)
3. If your intention is to format the entire disk, then using the format command couldn't be better. You'd better take a look at format help: format /?
Floor5 lianjiang2004 Posted 2009-02-23 18:04
金牌会员 Posts 1,884 Credits 3,946
The intention is unclear, hehe.

Floor6 moniuming Posted 2009-02-23 19:37
银牌会员 Posts 574 Credits 1,335 From 广西
If you're only checking one partition, the code below should work. If you want to check the entire hard drive, please modify it yourself...
Floor7 不得不爱 Posted 2009-02-23 20:08
超级版主 Posts 2,044 Credits 5,310 From 四川南充
Just modify the disk variable to the drive letter you need to check, and replace the echo line with the command you want to execute!
Floor8 binjie Posted 2009-02-23 23:16
新手上路 Posts 9 Credits 17
Many thanks to the moderator. Thanks.
Floor9 binjie Posted 2009-02-23 23:24
新手上路 Posts 9 Credits 17
Originally posted by 不得不爱 at 2009-2-23 08:08 PM:
Just modify the disk variable to the drive letter you need to check, and replace the echo statement line with the command you want to execute!
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023