0

I have a .bat script where I need to compare my free disk space with exactly 18GB. If it's lower or equal than 18GB, it should exit. If It's greater, it should continue.

@echo off

setlocal

set maxSize=19327352832

for /f "tokens=3" %%a in ('dir c:\') do (
    set bytesfree=%%a
)

set bytesfree=%bytesfree:,=%

Echo %bytesfree%
Echo %maxSize%

If %bytesfree% LEQ %maxSize% Echo You'll need to delete some stuff first & pause & exit
If %bytesfree% GTR %maxSize% Echo Everything ready

endlocal && set bytesfree=%bytesfree%

I have 5GB free on C:\, so it should say "Everything ready" but it says "You'll need to delete some stuff first" and I don't now what is wrong.

I'm very new to .bat so if I have unecessary code, please correct me.

Enomaos33
  • 3
  • 1
  • 5 is less than 18, isn't it ? – Marged Dec 30 '19 at 17:39
  • Numbers are limited to 32 bit. +/- 2 gig. –  Dec 30 '19 at 17:53
  • @Mark Really? Is there a way around to fix this? – Enomaos33 Dec 30 '19 at 17:55
  • Scale it in your head. Work in Megabytes. –  Dec 30 '19 at 17:59
  • 1
    Can you please also clarify how many bytes you've determined are representative of 18GB, _`19327352832` bytes = `18` Gibibytes (GiB) and `18000000000` bytes = `18` Gigabytes (GB)_. – Compo Dec 30 '19 at 18:02
  • @Marged: sadly, for `cmd` it's not. (well, 5 *is* less than 18, but it's different with 5000000000 and 18000000000) – Stephan Dec 30 '19 at 18:03
  • When CMD was first written 20 Megabytes was big. –  Dec 30 '19 at 18:08
  • @Stephan I was referring to `I have 5GB free on C:\ so it should say "Everything ready" ` which contradicts `If it's lower or equal than 18GB, it should exit` – Marged Dec 30 '19 at 19:47
  • 1
    Agreed @Marged, the OP's requirements were "If it's lower or equal than 18GB, it should exit", so as they "have 5GB free on C:", it should **not** say "Everything ready", and is technically providing the correct response! – Compo Dec 30 '19 at 19:57

2 Answers2

2

Perhaps a based would work for you:

@("%__AppDir__%wbem\WMIC.exe" LogicalDisk Where "DeviceID='C:' And FreeSpace>'18000000000'" Get FreeSpace /Value 2>NUL|"%__AppDir__%find.exe" "=">NUL&&(Echo Everything ready)||(Echo You'll need to delete some stuff first&"%__AppDir__%timeout.exe" /T 3 /NoBreak>NUL&Exit /B))&Pause

Change 18000000000 to 19327352832 as necessary.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Generally, I also prefer a method to get the desired information directly (`wmic` here) over parsing the output of a generic command (`dir` here), but in this case, `wmic` is incredibly slow (compared to the `dir` approach). Interesting though, that `wmic` isn't affected by the INT32 limit. – Stephan Dec 30 '19 at 21:04
  • Well, 'slow' for me @Stephan, _unless the computer is constantly moving files on and off the system drive_, is not how I'd term a result within a second or two, _which in my experience, this is_. – Compo Dec 30 '19 at 21:20
1

Your problem is, that numbers in batch are limited to INT32 (that's about 2GB), so if returns unexpected results. As a workaround you can compare the numbers as strings (you need to make sure, they have the same number of digits). Note dir's switch /-c, which suprresses the thousand separators:

@echo off
setlocal
set "maxSize=0000019327352832"
for /f "tokens=3" %%a in ('dir /-c c:\') do (
    set bytesfree=0000000000000000%%a
)
set bytesfree=%bytesfree:~-16%
Echo %bytesfree%
Echo %maxSize%

If "%bytesfree%" LEQ "%maxSize%" Echo You'll need to delete some stuff first & pause & exit
If "%bytesfree%" GTR "%maxSize%" Echo Everything ready

endlocal && set bytesfree=%bytesfree%

As an alternative, you could use the help of a "proper" programming language (like Powershell) for the math, but that's always slower and not really necessary here (as you can see)

Stephan
  • 53,940
  • 10
  • 58
  • 91