Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a way already to check the free space of a hard drive in a batch script?

I'd rather not use third party apps since I need to fill out a lot of forms, and in that case I think I'll write a small app myself.

share|improve this question

2 Answers 2

The easiest way to reliably get at the free disk space is using WMI. When trying to parse the output of dir you get all kinds of funny problems, at the very least with versions of Windows in other languages. You can use wmic to query the free space on a drive:

wmic logicaldisk where "DeviceID='C:'" get FreeSpace

This will output something like

FreeSpace
197890965504

You can force this into a single line by adding the /format:value switch:

> wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value

FreeSpace=197890965504

There are a few empty lines there, though (around three or four) which don't lend themselves well for processing. Luckily the for command can remove them for us when we do tokenizing:

for /f "usebackq delims== tokens=2" %x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%x

The nice thing here is that since we're only using the second token all empty lines (that don't have a second token) get ignored.

Remember to double the % signs when using this in a batch file:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x

You can now use the free space which is stored in the environment variable %FreeSpace%.

share|improve this answer

Here is a simple script to check your c-drive

echo C-drive Free Space >> CDrive.txt

dir c:\|find /i "bytes free" >> CDrive.txt

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.