JSI Tip 3687. Batch script returns drive size and freespace.


Using GetSysInfo.exe, I have scripted GetDriveInfo.BAT, which returns the following environment variables:

Environment   
Variable
 
 D e s c r i p t i o n 
 jsidrive   The drive letter that you queried. 
 jsisize   A character-based variable containing the drive size in bytes. 
 jsisizemb   An arithmetic variable containing the drive size in megabytes. 
 jsifree   A character-based variable containing the drive freespace in bytes. 
 jsifreemb   An arithmetic variable containing the drive freespace in megabytes. 

GetDriveInfo.bat has one parameter, the drive letter you are querying. If you ommit the parameter, drive C is queried. If you query an invalid drive letter, jsisize will be 0.

GetDriveInfo.BAT contains:

@echo off
if \{%1\}==\{\} set jsidrive=C&goto getdrive
set jsidrive=%1
set jsidrive=%jsidrive:"=%
set jsidrive=%jsidrive::=%
set jsidrive=%jsidrive:\=%
:getdrive
set jsisize=0
set jsifree=0
set /a jsisizemb=0
set /a jsifreemb=0
for /f "Skip=8 Tokens=1-3 Delims=:\" %%i in ('getsysinfo.exe') do call :parse %%i %%j %%k 
set len=
set char=
goto :EOF
:parse
if /i "%jsidrive%" NEQ "%1" GOTO :EOF
set jsisize=%2#
set jsifree=%3#
set /a len=0
:parse1
set /a len=%len% + 1
for /f %%n in ('@echo %%jsisize:~%len%^,1%%') do set char=%%n
If "%char%" NEQ "#" goto parse1
set jsisize=%jsisize:#=%
set /a len=%len% - 6
for /f %%n in ('@echo %%jsisize:~0^,%len%%%') do set jsisizemb=%%n
set /a jsisizemb=%jsisizemb%
set /a len=0
:parse2
set /a len=%len% + 1
for /f %%n in ('@echo %%jsifree:~%len%^,1%%') do set char=%%n
If "%char%" NEQ "#" goto parse2
set jsifree=%jsifree:#=%
set /a len=%len% - 6
for /f %%n in ('@echo %%jsifree:~0^,%len%%%') do set jsifreemb=%%n
set /a jsifreemb=%jsifreemb%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish