0

How can i get the drives Total Space and Available space by running an .bat file. In powershell script it is possible. But i should not use power shell. In the basic script programming i need to get that. I had tried below code, But its not giving proper result

fsutil volume diskfree C:\>temp.txt
FOR /F "Tokens=* skip=1 delims= " %%A IN (temp.txt) DO echo %%A>>temp2.txt
SORT /+32 temp2.txt /O temp3.txt
FOR /F "tokens=5 Delims=: " %%A IN (temp3.txt) DO ECHO %%A>temp4.txt
FOR /f "tokens=1 Delims= " %%A IN (temp4.txt) DO SET a=%%A
set /a b=%a:~0,-10%
set /a c=b*1024
PAUSE
DEL temp.txt
DEL temp1.txt
DEL temp2.txt
DEL temp3.txt
DEL temp4.txt
CLS
ECHO %b% GB (%c% MB)
PAUSE
RobinHood
  • 2,099
  • 10
  • 39
  • 67

2 Answers2

3

Going off of Foxidrive's answer, I've had it enumerate all diskdrives and output the size for each the way you specified in your script

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1" %%d in (
 'wmic logicaldisk where drivetype^=3 get deviceid ^| find ":"') do ( 
    for /f "skip=1 tokens=1,* delims=:" %%a in ('fsutil volume diskfree %%d') do (
        Call :ConvertBytes %%b GB Gigs
        Call :ConvertBytes %%b MB Megs
        echo %%d - %%a: !Gigs! GB (^!Megs! MB^) >> output.txt
    )
)       
goto :eof


:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),0)
for /f "delims=" %%a in ( 
  'cscript //nologo %temp%\tmp.vbs' 
) do endlocal & set %~3=%%a
del %temp%\tmp.vbs
Matt Williamson
  • 6,687
  • 1
  • 20
  • 33
  • Exactly returning the values..Thanks a lot....How to write this output values in a text file.? – RobinHood May 23 '13 at 06:56
  • +1, I like the use of WMIC, and good solution for arithmetic on large numbers. It could be done easily without a temporary file if you switch from VBS to JScript. See [hybrid JScript/batch scripting](http://stackoverflow.com/a/5656250/1012053). It is also possible to to do with VBS. See [hybrid VBS/batch scripting](http://stackoverflow.com/q/9074476/1012053). But I think JScript is a better option. – dbenham May 23 '13 at 12:20
1

This works here using your tools. I may have misunderstood - you didn't say you want to calculate GB etc.

@echo off
for /f "skip=1 tokens=1,* delims=:" %%a in ('fsutil volume diskfree c:') do echo %%a - %%b
pause
foxidrive
  • 37,659
  • 8
  • 47
  • 67