Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to get total size of a disk or partition(a USB hard drive with 1 NTFS partition) in a batch file?
I would rather not go for logic that go over files recursively(the ones with for /f) since it's a 1TB drive and it may take a long time to determine size.

I am open to use some small third party tool which will return size to batch file.

PS: I am on Windows 7 x64 Ultimate

share|improve this question
I'm assuming Windows OS from the question text but what version? Could you please update the question tags and text. – Julian Knight Feb 26 '13 at 22:31

There is probably an easier way to do this, but here is what I use

If all you want is the bytes, you can just do this command change T to the drive letter of your external (for a one liner at command prompt change %% to % and set space= to echo)

for /f "tokens=3 delims= " %%a in ('dir /-c T: ^| find "bytes free"') do set space=%%a

if you want to then take that and simplfy it, first set guidelines for each size

set divide=1073741824&&set size=gigabytes
if %space% lss 1073741824 set divide=1048576&&set size=megabytes
if %space% lss 1048576 set divide=1024&&set size=kilobytes
if %space% lss 1024 set divide=1&&set size=bytes

Then write a temporary VBS script as batch can't handle the math

echo wsh.echo cdbl(%space%)/%divide% > %temp%\tmp.vbs

Run it, capture result and delete it

for /f %%a in ('cscript //nologo %temp%\tmp.vbs') do set space=%%a
del %temp%.\tmp.vbs

Then the following trims the result to two decimal places

for /f "tokens=1,2 delims=." %%a in ("%space%") do (
  set s1=%%a
  set s2=%%b
if not "%s2%"=="" (set space=%s1%.%s2:~0,2%) else (set space=%s1%)

Echo results

echo Drive has %space% %size% free.


For total size, you can use diskpart list volume (change "%%a"=="T" to "%%a"=="driveletterhere")

for /f "tokens=3,6,7 delims= " %%a in ('echo list volume ^| diskpart') do if "%%a"=="T" set size=%%b && set size2=%%c

and bonus how you could calculate that:

if [%size2%]==[TB] (
    set isBigEnough=1
) else if [%size2%]==[GB] (
    if %size% GTR 31 set isBigEnough=1
if not defined isBigEnough set isBigEnough=0
share|improve this answer
I believe this is for free space. I want for total size. Basically I want to execute some command only if drive size is less than 32GB. I suppose I wont have to do the math then.. – tumchaaditya Feb 27 '13 at 11:11
That's actually easier, see my edit for solution for total size. – Patrick Meinecke Feb 27 '13 at 18:58
Thanks man..Just an edit..tokens need to be 3,7,8 in my case..may be diskpart is showing an extra more thing...i did not quite understand the part "in ('echo list volume ^| diskpart')" does it work? because if i am to do it manually, i will first have to run diskpart and then list does it pipe the list volume inside diskpart? – tumchaaditya Feb 28 '13 at 7:46
if you type echo list volume | diskpart you will see that list volume is echoed into diskpart, it can be done from command line as well as long as you unescape |. And not sure why the tokens are different for you, but I'm glad you could figure it out. – Patrick Meinecke Feb 28 '13 at 15:12
another problem..volume name may or may not contain delims change every time – tumchaaditya Mar 1 '13 at 18:04

Maybe try df.exe which is available from and other locations. This is a copy of a UNIX command line tool.

You should also be able to get the value direct from the Windows Management Interface (WMI) though I'm afraid my skills in that area are not good enough to give a direct answer.

fsutil volume diskfree c: will give you 3 lines of output that you could probably use a find on to get the result you want.

Example output:

Total # of free bytes        : 466333454336
Total # of bytes             : 972999880704
Total # of avail free bytes  : 466333454336

fsutil volume diskfree c: | find "Total # of bytes" gives you a specific line.

for /F "tokens=2 delims=:" %i in ('fsutil volume diskfree c: ^| find "Total # of bytes"') do @set USED=%i

gives you the number with a preceding space.

For a comparison:

@REM 32MB = 32*1024*1024 bytes
@set COMP=33554432

@REM Pull out the line containing the number of bytes USED
for /F "tokens=2 delims=:" %i in ('fsutil volume diskfree c: ^| find "Total # of bytes"') do @set USED=%i

@REM Drop the leading space character to just get the number so a numeric comparison is done
for /F "tokens=1 delims= " %i in ('echo %USED%') do @set USED=%i

@REM Tell me something useful
if %USED% lss %COMP% @echo "Used is less than 32MB"
if %USED% gtr %COMP% @echo "Used is greater than 32MB"

NOTE: fsutil requires admin rights so this may not work on locked-down computers.

share|improve this answer
Thanks man. fsutil thing works. I devised a procedure to get the final size out(just a for /f). I'll add that stuff to your answer and mark it as accepted. – tumchaaditya Feb 27 '13 at 11:24
I have updated the solution. But the thing I want to do further is compare this size to a number(32GB converted to bytes to be specific) and execute a command if the size is less than 32GB. Any idea how to compare this obtained string to a number? – tumchaaditya Feb 27 '13 at 11:49
Everything sounds perfect except that I would get free space size. But, I need full drive size.. – tumchaaditya Feb 27 '13 at 14:08
@tumchaaditya that is the last line in the output of the fsutil command. Just change the find text to match the appropriate line. – Julian Knight Feb 28 '13 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .