How to find free space using batch code in GB

March 19, 2010 at 20:49:56
Specs: Windows Vista
How to find free space on my computer using batch code in GB. My operating system is Windows Vista

See More: How to find free space using batch code in GB

Report •

#1
March 20, 2010 at 01:18:02
I don't have vista, but the below works for me on xp. It's a little
slow and only works on ntfs volumes......

Set "numplace" to your desired number of decimal places(set to 2).

No rounding is performed.

@ECHO OFF
setlocal EnableDelayedExpansion
echo Please wait this could take some time.
:begin
set n=1
for /f "delims=" %%a in ('fsutil fsinfo drives^|find /v ""') do (
    set drive=%%a
    set drive=!drive:drives: =!
    set list!n!=
    for /f "tokens=2 delims=:" %%b in ('fsutil volume diskfree !drive!^|find /v "ready"') do (
        set huh=%%b
        set huh=!huh:~1,-1!
        for %%c in (!n!) do set list%%c=!list%%c!!huh!,
    )
    if defined list!n! (
        for %%c in (!n!) do set list%%c=!list%%c!!drive!
        set /a n+=1
    )
)
set /a n-=1
echo +++++ Local NTFS Volumes +++++

rem numplace is the number of decimal places
set numplace=2

for /l %%a in (1 1 !n!) do (
    for /f "tokens=1-4 delims=," %%b in ("!list%%a!") do (
        call :bigmath %%c / 1073741824 total
        call :bigmath %%d / 1073741824 avail
        call :bigmath %%c - %%d used
        call :bigmath !used! / 1073741824 used
        echo %%e - !total!gb Capicity - !used!gb Used - !avail!gb Available
    )
)
pause

goto :eof


rem stripped out half of the functionality
:bigmath
SETLOCAL ENABLEDELAYEDEXPANSION
set optr=%~2
set inpnum1=%1
set inpnum2=0
set dplace=
set varout=%~3
IF "%~3"=="" ENDLOCAL&GOTO :eof
SET VAROUT=%~4
set inpnum2=%3
if "%inpnum1%"=="%inpnum1:.=%" if "%inpnum2%"=="%inpnum2:.=%" goto FLOATSKIP
if "%inpnum1:~0,1%"=="." set inpnum1=0%inpnum1%
if "%inpnum2:~0,1%"=="." set inpnum2=0%inpnum2%
if "%inpnum1:~-1%"=="." set inpnum1=%inpnum1%0
if "%inpnum2:~-1%"=="." set inpnum2=%inpnum2%0
SET DP1=0
SET DP2=0
FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM1%") DO (
	SET DPT=%%A
	FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP1=%%B&&GOTO BL1
)
:BL1
FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM2%") DO (
	SET DPT=%%A
	FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP2=%%B&&GOTO BL2
)
:BL2
SET DPLACE=%DP1%
IF NOT !DP1! EQU !DP2! (
	IF !DP1! LSS !DP2! (
		SET DPLACE=%dp2%
		SET /A DPT=DP2-DP1
		FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM1=!INPNUM1!0
	) ELSE (
		set dlace=%dp1%
		SET /A DPT=DP1-DP2
		FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM2=!INPNUM2!0
	)
)

SET INPNUM1=%INPNUM1:.=%
SET INPNUM2=%INPNUM2:.=%
:FLOATSKIP
:dzer01
if not "!inpnum1:~1!"=="" if "!inpnum1:~0,1!"=="0" (
    set inpnum1=!inpnum1:~1!
    goto dzer01
)
:dzer02
if not "!inpnum2:~1!"=="" if "!inpnum2:~0,1!"=="0" (
    set inpnum2=!inpnum2:~1!
    goto dzer02
)


if "!optr!"=="-" goto subtract
if "!optr!"=="+" goto add
if "!optr!"=="*" goto multiply
if "!optr!"=="/" goto divide
goto :eof

:subtract
for %%a in (stotal stten SUB1 SUB2 neg) do set %%a=
if /I "%~1%~2"=="internal" (
	set sub1=!%3!
	set sub2=!%4!
) else (
	set sub1=!inpNUM1!
	set sub2=!inpNUM2!
)

set subsub1=!sub2!
set subsub2=!sub1!
:sloop
set sc1=
set sc2=
set sstrore=
if defined sub1 (
	if defined sub2 (
		set sc1=%sub1:~-1%
		set sc2=%sub2:~-1%
		set sub1=%sub1:~0,-1%
		set sub2=%sub2:~0,-1%
	) else (
		if defined stten (
			set sub2=!stten!
			set stten=
			set sc1=!sub1:~-1!
			set sc2=!sub2:~-1!
			set sub1=!sub1:~0,-1!
			set sub2=!sub2:~0,-1!
		) else (
			set stotal=%sub1%%stotal%
            call :finish stotal
			goto soutput
		)
	)
) else (
	for %%a in (sub2 stten) do (
		if defined %%a (
			set sub1=%subsub1%
			set sub2=%subsub2%
			set neg=-
			set stotal=
			set stten=
			goto sloop
		)
	)
    call :finish stotal
	goto soutput
)
set sazc=
set sazcnt=
:asttend
if !sc1!==0 if !sc2! gtr 0 if defined sub1 (
	set sazc=!sub1:~-1!
	set /a sazcnt+=1
	set sub1=!sub1:~0,-1!
	if !sazc!==0 goto asttend
	set /a sazc-=1
	set sc1=10
	set sub1=!sub1!!sazc!
	for /l %%a in (2,1,!sazcnt!) do set sub1=!sub1!9
)
if defined stten (
	set /a sc1-=1
	if !sc1! lss 0 (
		set /a stten+=1
		set /a sc1+=10
	) else (
		set stten=
	)
)
set /a sstrore=%sc1%-%sc2%
if !sstrore! lss 0 (
	set stten=1
	set /a sstrore+=10
	set stten=!stten:-=!
)
set stotal=%sstrore%%stotal%
goto sloop
:soutput
if /I "%~1%~2"=="internal" (
	set %3=%stotal%
    goto :eof
) else (
    if not defined varout (
    	echo %stotal%
    	ENDLOCAL
    ) else (
    	endlocal&set %varout%=%stotal%
    )
)    
goto :eof

:add
if /I "%~1%~2"=="internal" (
	set add1=!%3!
	set add2=!%4!
) else (
	set add1=!inpNUM1!
	set add2=!inpNUM2!
)
set atotal=
set astore=
set acarry=
:aloop
if not defined add1 (
	if defined acarry (
		set add1=%acarry%
		set acarry=
	) else (
		set atotal=%add2%%atotal%
		call :finish atotal
		goto aoutput
	)
)
if not defined add2 (
	if defined acarry (
		set add2=%acarry%
		set acarry=
	) else (
		set atotal=%add1%%atotal%
		call :finish atotal
		goto aoutput
	)
)
if not defined acarry (
	set /a astore=%add1:~-1%+%add2:~-1%
) else (
	set /a astore=%add1:~-1%+%add2:~-1%+%acarry%
	set acarry=
)
if !astore! lss 10 (
	set atotal=%astore%%atotal%
) else (
	set atotal=%astore:~-1%%atotal%
	set acarry=%astore:~0,-1%
)
set astore=
set add1=%add1:~0,-1%
set add2=%add2:~0,-1%
goto aloop
:aoutput
if /I "%~1%~2"=="internal" (
	set %3=%atotal%
) else (
	if not defined varout (
		echo %atotal%
		ENDLOCAL
	) else (
		endlocal&set %varout%=%atotal%
	)
)
goto :eof



:divide
if !inpnum2! equ 1 (
	set dtotal=%inpnum1%
	call :finish dtotal
	goto doutput
)
for %%a in (din1 din2 diff dten DCNT) do set %%a=
set dtotal=0
if not defined numplace set numplace=10
set dplace=!numplace!
for /l %%a in (1 1 !numplace!) do set inpnum1=!inpnum1!0
set div1=!inpnum1!
set div2=!inpnum2!
if !div2!==0 echo Divide by 0^^!>&2&&exit /b 2
:din1
if defined inpnum1 (
	set /a din1+=1
	set inpnum1=%inpnum1:~1%
	goto din1
)
:din2
if defined inpnum2 (
	set /a din2+=1
	set inpnum2=%inpnum2:~1%
	goto din2
)
if !din2! gtr !din1! (
set dtotal=0
goto doutput
)
set /a diff=din1-din2
for /l %%a in (1,1,%diff%) do set dten=!dten!0
set ph=!dplace!
set dplace=
:div
set dtmp=%div1%
set daddvar=%div2%%dten%
call :subtract inte rnal div1 daddvar
if defined neg (
	set div1=!dtmp!
	set neg=
	if defined dten (
		set dten=!dten:~1!
		goto div
	) else (
		set dplace=!ph!
		call :finish dtotal
		goto doutput
	)
) else (
	set daddvar=1%dten%
	call :add inte rnal dtotal daddvar
	if !div1! equ 0 (
	set dplace=!ph!
	call :finish dtotal
	goto doutput
)
goto div
)
:doutput
if not defined varout (
	echo %dtotal%
	ENDLOCAL
) else (
	endlocal&set %varout%=%dtotal%
)
goto :eof


:finish
if !dplace! equ 0 set dplace=
if defined dplace if "!%1:~%dplace%,1!"=="" set %1=0!%1!&&goto finish
if defined dplace set %1=!%1:~0,-%dplace%!.!%1:~-%dplace%!
:killzerodec
if defined dplace (
	if "!%1:~-1!"=="0" (
		set %1=!%1:~0,-1!
		goto killzerodec
	) else (
		if "!%1:~-1!"=="." (
			set %1=!%1:~0,-1!
		)
	)
)
:killzerowhle
if "!%1:~0,1!"=="0" (
		set %1=!%1:~1!
		goto killzerowhle
	) else (
		if "!%1:~0,1!"=="." set %1=0!%1!
	)
IF NOT DEFINED %1 SET %1=0
set %1=%neg%!%1!
goto :eof


Report •

#2
March 20, 2010 at 02:12:36
Good grief, Brownly Char

-------------------------------
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=3 delims= " %%a in ('dir x:\ ^| find "bytes free"') do (
set G=%%a
set G=!G:,=!
set G=!G:~0,-9!
echo.!G!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#3
March 20, 2010 at 02:26:49
Hi M2,

Good grief, Brownly Char

Yes very over the top, but also very accurate.........


Report •

Related Solutions

#4
March 20, 2010 at 10:54:00
Hi

First of all thanks for your help.

Can i get the out put in text file, i am working on maintainance project so i need to check the disk space(in GB) avliable daily & report to my senior.

Right now i am geeting in bytes(disk space).

Once again thanks for the help.

Regards
Sridhar


Report •

#5
March 20, 2010 at 22:33:42
echo.!G! >> log.txt


=====================================
Helping others achieve escape felicity

M2


Report •

#6
March 21, 2010 at 06:20:27
I'm not worth a hi any more M2?
Excuse me while I curl up any cry :( (Only joking ;))

Same goes for extravagant, 300 line+, script:

        >> SOMEFILE echo %%e - !total!gb Capicity - !used!gb Used - !avail!gb 


Report •

#7
March 21, 2010 at 06:46:37
Hi Judago

:)


=====================================
Helping others achieve escape felicity

M2


Report •

#8
March 21, 2010 at 08:33:15
Hi

I don't have knowledge on Batch Coding.
So can you kindly help in this.

I tried up with what you have shared but nothing come's up no output.

Thanks
Sridhar


Report •

#9
March 21, 2010 at 15:40:30
... well euhm, what happens when you run this command :

dir c:\


Report •

#10
March 21, 2010 at 17:02:46

Report •

#11
March 21, 2010 at 22:08:18
It gives me the list of files & dir in c drive and the space avaliable.

Regards
Sridhar


Report •

#12
March 22, 2010 at 03:50:15
Try this:

C:\>type script1.cmd
@echo off
setlocal enabledelayedexpansion

for /f "tokens=3" %%f in ('dir c:\ ^| findstr /C:"bytes free" ^| findstr /C:"Dir(s)"') do (
set t=%%f
echo !t!
set t=!t:.=!
echo !t!
set t=!t:~0,-6!
echo !t!
set /a t=%t/1024
echo !t!
)


C:\>script1.cmd
61.825.409.024
61825409024
61825
60
C:\>

The accuracy is not very good, but there is this problem I'm facing :

C:\>set /a x=61825376256/(1024*1024)
Invalid number. Numbers are limited to 32-bits of precision.

C:\>


Report •

Ask Question