Get disk/partition size in batch file

8,695

Solution 1

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.

[EDIT]

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

Solution 2

Maybe try df.exe which is available from http://unxutils.sourceforge.net/ 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:
8,695

Related videos on Youtube

tumchaaditya
Author by

tumchaaditya

I am a software developer from Pune, India. I am working on ENOVIA LCA customization using CAA. I have dabbled in MS Excel VBA programming and Windows Phone application development as well

Updated on September 18, 2022

Comments

  • tumchaaditya
    tumchaaditya over 1 year

    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

  • tumchaaditya
    tumchaaditya about 11 years
    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
    tumchaaditya about 11 years
    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
    tumchaaditya about 11 years
    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
    tumchaaditya about 11 years
    Everything sounds perfect except that I would get free space size. But, I need full drive size..
  • Patrick Meinecke
    Patrick Meinecke about 11 years
    That's actually easier, see my edit for solution for total size.
  • Julian Knight
    Julian Knight about 11 years
    @tumchaaditya that is the last line in the output of the fsutil command. Just change the find text to match the appropriate line.
  • Patrick Meinecke
    Patrick Meinecke about 11 years
    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.
  • tumchaaditya
    tumchaaditya about 11 years
    another problem..volume name may or may not contain spaces...so delims change every time