9

I'm trying to create a batch file to pull the total size and free space of the C:\ drive of servers (locally run script). I also need the output to be easily readable, so bytes is not going to work, so I'm ok with having a command line that creates a temp .vbs file.

The following seems like it could work, but the formatting/math isn't correct.

setlocal
for /f "tokens=6" %a in ('fsutil volume diskfree C: ^| find "of bytes"') do set diskspace=%a
echo wsh.echo FormatNumber(cdbl(%diskspace%)/1024, 0) > %temp%.\tmp.vbs
for /f %a in ('cscript //nologo %temp%.\tmp.vbs') do set diskspace=%a
del %temp%.\tmp.vbs
echo For example %diskspace%

The above commands are also only showing free space... I would like total size too... Wondering if the following command might be better for pulling the info:

WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:"

Note also that I want this to be able to be copy/pasted directly into a command prompt (not a batch file - forced requirements). I've already removed the "%%"'s from the code above.

Note: Needs to run natively on Server 2003+ (so Powershell is out, as well as any 3rd party utils).

  • what version of windows? – EBGreen Apr 1 '15 at 16:51
  • Server 2003+, natively. Update my original post to reflect this. – BondUniverse Apr 1 '15 at 17:06
9

Under the not a batch file - forced requirements clause, next cmd one-liner could help:

for /f "tokens=1-3" %a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%b" ^& " free=" ^& FormatNumber^(cdbl^(%a^)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%c^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%c"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs

Output:

==>for /f "tokens=1-3" %a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FIND
STR /I /V "Name"') do @echo wsh.echo "%b" ^& " free=" ^& FormatNumber^(cdbl^(%a^
)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%c^)/1024/1024/
1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%c"=="" @echo( & @cscript //nolog
o %temp%\tmp.vbs & del %temp%\tmp.vbs

C: free=79,11 GiB size=111,45 GiB

D: free=929,47 GiB size=931,51 GiB

==>
9

I realize you're looking as VBS right now, but PowerShell can do this very easily:

$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size, FreeSpace

Write-Host ("{0}GB total" -f [math]::truncate($disk.Size / 1GB))
Write-Host ("{0}GB free" -f [math]::truncate($disk.FreeSpace / 1GB))

First line gets the disk info from WMI (just does C: in this example), and selects just the Free Space and Total sizes.

In the next two lines write the Free Space and Total sizes to the console, formatted to be in GB, with fractions truncated off.

Example output (as-is):

223GB total
125GB free
  • Forgot to put a note about that... Need it to work natively on Server 2003+ (since Powershell isn't native in 2003, can't use it). Definitely would have been easier with Powershell though! – BondUniverse Apr 1 '15 at 17:05
2

Divide by 1024 to get kibytes (2^10). Divide by 1024 x 1024 to get Mibytes (2^20). Divide by 1024 x 1024 x 1024 to get Gibytes (2^30).

Windows calls these units kilobytes, Megabytes, and Gigabytes for historical reasons. I think my casing is right.

So change your sums.

2

In a batch file, you can get the disk size in GB of a specified disk by using a DiskPart script. In this example, I'm setting the specific disk index according to its model name (assuming there's only one like it in the computer). Change the string "SATA Disk" to match your disk drive's model. You can get the model name by running the command "wmic diskdrive get Model, Index" in cmd.

Of course, you can use a different property of the disk in the wmic command - type wmic diskdrive get /? to get a list of all disk properties you can use.

for /f %%I in ('wmic diskdrive get Model^, Index ^| find "SATA Disk"') do set DISK_Index=%%I

if not exist %~dp0Disk_Size.script (echo list disk > %~dp0Disk_Size.script)
@echo off
FOR /F "tokens=4 delims= " %%J in ('diskpart /s %~dp0Disk_Size.script ^| find "Disk %DISK_Index%"') do set DISK_SIZE=%%J
IF %ERRORLEVEL% EQU 1 (echo No Data was processed, cannot get DISK_SIZE)

echo DISK_SIZE is %DISK_SIZE% GB
2

If you just need to check if the drive have enough free space (to install or copy something for example) you can try wmic query like this:

wmic LogicalDisk where "DeviceID='c:' and FreeSpace > 10737418240" get DeviceID 2>&1 ^ | find /i "c:" >nul || (echo not enough space&exit 1)

this will break batch file execution if there is less than 10Gb free on C:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.