In a .bat file, how can I test available disk space?
I suggest using a PowerShell script for that.
wmic
returns the size in bytes, batch is limited to number comparisons of signed 32-bit integers, far below 100GB. You would have to truncate the number first.
PowerShell
if ((Get-WMIObject Win32_Logicaldisk -filter "deviceid='C:'").FreeSpace -gt 100GB){
"yes enough free space"
} else {
"not enough free space"
}
I have to extend the above information about number comparison in batch.
If numbers are contained in equal length strings they can be validly compared as strings.
A string comparison takes place character by character, a leading space/zero is less than 1.
At the moment the largest hard drives size is 10TB = 10995116277760 bytes taking 14 decimal places - the following batch uses 15 places for all numbers.
@Echo off
Rem 543210987654321
Set "Blank= "
Set "GB100= 107374182400"
Set "TB_10= 10995116277760"
for /f "tokens=2" %%A in (
'wmic LogicalDisk Get DeviceID^,FreeSpace ^| find /i "C:"'
) Do Set "FreeSpace=%Blank%%%A"
Set "FreeSpace=%FreeSpace:~-15%"
Echo FreeSpace="%FreeSpace%"
Echo 100 GB="%GB100%"
If "%FreeSpace%" gtr "%GB100%" (
Echo yes enough free space
) else (
Echo not enough free space
)
Sample output:
> Check-FreeSpace.cmd
FreeSpace=" 101606346752"
100 GB=" 107374182400"
not enough free space
> Check-FreeSpace.cmd
FreeSpace=" 1181504520192"
100 GB=" 107374182400"
yes enough free space
The simplest way is to look at the last line in dir
output
dir /-c /w C: | find "bytes free"
You can also get the size value directly by getting the 3rd token in the output
FOR /F "tokens=3 USEBACKQ" %%F IN (`dir /-c /w C:`) DO set "size=%%F"
echo %size%
However it's not reliable as the string may be different in another language. And also note that sometimes you have a lot of freespace but can't write any more files because you've reached some limits like maximum file size or maximum number of files in a volume/directory...
The most correct way is
wmic LogicalDisk where DeviceID='C:' Get FreeSpace /value