7

I have a .bat script, which starts on powering up a dedicated backup server, saves the productive environment using Robocopy, and shuts down the server when done.

It does some checks before saving, and requests appropriate measures when something is not OK, for example when the target JBOD case was not detected due to being powered off.

There is one more caveat, though, which I was not able to handle so far.

How can I test in the .bat file, whether I have enough space on the target disks to receive all the files or not? (It's OK to test for a fixed space size, modified periodically; currently it's near to 100 GB.)

Is it even possible? If so, what command do I need to research?

2 Answers 2

12

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
2
  • 1
    @Bob Stuff and nonsense, 15 decimal places will hold ~909.5TB quite near the presently not available 1PB - but the concept is clear and nobody keeps you from using 16 places to get ~8.88PB
    – LotPings
    May 21, 2017 at 6:33
  • 1
    Hm. It didn't quite click in my head that you'd gone one digit past the 10 TB value, sorry.
    – Bob
    May 21, 2017 at 8:06
9

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
2
  • I suspect the find /i "C:" part would fail to do what you expect if you have multiple partitions mounted on paths beginning with C:. Windows has allowed that since 2000-ish, but it isn't a very commonly used feature, so this might not be a problem in the OP's case, but it probably isn't "the most correct way" to do it.
    – user
    May 20, 2017 at 20:51
  • @MichaelKjörling if the drive is mounted in path\to\mountpoint then dir /-c /w path\to\mountpoint | find "bytes free" still works. You may need a supporting script with mountvol if you want it to work with wmic
    – phuclv
    May 21, 2017 at 4:57

You must log in to answer this question.

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