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-08-11 13:41
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Need a batch file to get the remaining free space on a hard drive. View 865 Replies 8
Original Poster Posted 2009-02-23 17:03 ·  中国 广西 北海 电信
新手上路
Credits 17
Posts 9
Joined 2008-04-03 23:09
18-year member
UID 114905
Gender Male
Status Offline
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!
Floor 2 Posted 2009-02-23 17:35 ·  美国 华盛顿州 金 雷德蒙德 Microsoft
初级用户
★★
Credits 100
Posts 93
Joined 2009-01-14 15:04
17-year member
UID 136661
Gender Male
From 湖南
Status Offline
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.
@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=
MD %%i:\test
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 The rest disksize of the prartition %%i is !num3_str! MB
echo.
)
RD %%i:\test
)
)
pause


:Loop
REM division
REM
SET /a num3=%num4%%num1:~0,1%/%num2%

REM The return value
REM
SET num3_str=%num3_str%%num3%

REM arithmetical compliment
REM
SET /a num4=%num4%%num1:~0,1%%%%num2%
IF %num4% EQU 0 SET num4=

REM Set num1
REM
SET num1=%num1:~1%
IF NOT "%num1%"=="" GOTO Loop
GOTO :EOF
Floor 3 Posted 2009-02-23 17:49 ·  中国 广西 北海 电信
新手上路
Credits 17
Posts 9
Joined 2008-04-03 23:09
18-year member
UID 114905
Gender Male
Status Offline
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
Floor 4 Posted 2009-02-23 18:01 ·  美国 华盛顿州 金 雷德蒙德 Microsoft
初级用户
★★
Credits 100
Posts 93
Joined 2009-01-14 15:04
17-year member
UID 136661
Gender Male
From 湖南
Status Offline
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 /?
Floor 5 Posted 2009-02-23 18:04 ·  中国 安徽 马鞍山 联通
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
The intention is unclear, hehe.

Windows 一键还原
http://www.yjhy.com
Floor 6 Posted 2009-02-23 19:37 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
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...
@echo off
set disk=d:
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where "caption='%disk%'" get FreeSpace /value') do (
set "sth=%%a"
)
set "sth=%sth:~,-7%"
echo %sth%
if %sth% lss 20 (
echo del something%If the displayed result meets your requirements, change this line to the delete command%
) else (
echo The disk capacity is already sufficient
)
echo.&pause
Floor 7 Posted 2009-02-23 20:08 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
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!
@echo off&color b
set disk=E
for /f %%a in ('wmic logicaldisk where "caption='%disk%:'" get FreeSpace /value 2^>nul') do SET %%a>nul
set free=%FreeSpace:~-10%
if not %FreeSpace:~0,-10%.==. goto GreaterThan
:StripLeading0
if %free:~0,1%==0 set free=%free:~1%&goto StripLeading0
if %free% gtr 20971520 goto GreaterThan
:LessThan
echo drive %disk% is less than 20M
pause
goto :eof
:GreaterThan
echo drive %disk% is greater than 20M
pause
Floor 8 Posted 2009-02-23 23:16 ·  中国 广西 北海 电信
新手上路
Credits 17
Posts 9
Joined 2008-04-03 23:09
18-year member
UID 114905
Gender Male
Status Offline
Many thanks to the moderator. Thanks.
Floor 9 Posted 2009-02-23 23:24 ·  中国 广西 北海 电信
新手上路
Credits 17
Posts 9
Joined 2008-04-03 23:09
18-year member
UID 114905
Gender Male
Status Offline
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!
@echo off&color b
set disk=E
for /f %%a in ('wmic logicaldisk wh ...



It doesn't work under PE,
Can you give one that can run under PE?
Forum Jump: