JSI Tip 1080. A better way to determine the freespace on a disk drive, in batch.
February 16, 1999
In tip 1071, we used CHKDSK in a batch script, to determine the free space on a disk drive.
Thanks to Chris Dickerson, we can use DF.
Typing DF /? at a command prompt, displays:
Chris Dickerson (R) DiskFree Version 1.00.080Copyright (c) Chris Dickerson 1999. All rights reserved.Free for use by anyoneDF Display space info on all fixed system drivesDF /? HelpDF [path] UNC or regular path, displays freespace available to youDF [path] [bytes] In a batch file, returns a "1" if there is enough space for [bytes]
Typing DF, displays information similar to:
Chris Dickerson (R) DiskFree Version 1.00.080Copyright (c) Chris Dickerson 1999. All rights reserved.Free for use by anyoneDrive free total capacity----- ---------------- ---------------- --------C: 817,383,424 2,138,540,032 38.22 %D: 1,228,214,272 2,146,764,800 57.21 %
Typing DF (c: or \ServerC$) displays:
Chris Dickerson (R) DiskFree Version 1.00.080Copyright (c) Chris Dickerson 1999. All rights reserved.Free for use by anyoneTotal free bytes available to you: 816,039,936Total bytes available to you : 2,138,540,032
Typing DF BytesRequired sets the ERRRORLEVEL to 1 if there is enough room for BytesRequired, or 0 if there is not. Using the above display:
DF C: 500000 sets ERRORLEVEL to 1.
DF C: 900000 sets ERRORLEVEL to 0
I have scripted DFree.bat to return the following environment variables:
Name | Type | D e s c r i p t i o n |
---|---|---|
DRV_TOT | numeric | The total drive size, in bytes. |
DRV_FREE | numeric | The free space available, in bytes |
The syntax is:
DFree
Using the above C: display, DRV_FREE is set to 817383424 and DRV_TOT is set to 2138540032.
DFree.bat contains:
@echo offif not "%1"
"" goto begin@echo Syntax: DF DriveLettergoto end:beginfor /f "Skip=4 Tokens=1,2 Delims=:" %%i in ('df.exe %1') do call :parse "%%i" "%%j"goto end:parseif /i %1
"Total free bytes available to you" goto freeset DRV_TOT=%2set DRV_TOT=%DRV_TOT: =%set DRV_TOT=%DRV_TOT:"=%set DRV_TOT=%DRV_TOT:,=%set /a DRV_TOT=%DRV_TOT%goto end:freeset DRV_FREE=%2set DRV_FREE=%DRV_FREE: =%set DRV_FREE=%DRV_FREE:"=%set DRV_FREE=%DRV_FREE:,=%set /a DRV_FREE=%DRV_FREE%:end
About the Author
You May Also Like