Comparing two numbers in DOS Batch Not Working

I am an old timer who is new to DOS Batch programming. I have what I think is a very simple script batch that doesn't work. I searched for similar posts and did not find what matched.

I am running the below script on XP. My goal is to check the free disk space before continuing, but I ran into the problem of comparing two numbers, so the script below contains only this logic. I have hardcoded numbers to show a problem that ... Comparison (if x gtr y) doesn't seem to work, so the branching logic goes the wrong way. Either that, or I'll get confused somewhere else in the IF statement. (Some of the echo instructions are not needed - they are for debugging, but I left them now.)

Any enlightenment about where I would be wrong would be greatly appreciated.

thanks...

@echo off set Free=217522712576 set Need=20000000000 echo Free=%Free% echo Need=%Need% echo on IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space @echo off :Insufficient_Space @ECHO INSUFFICIENT SPACE GOTO DONE :Sufficient_Space @ECHO SUFFICIENT SPACE :DONE 
+6
source share
3 answers

These numbers would overwhelm the 32-bit integer, so guessing at the 32-bit version of windows why this fails.

 C:\>set /a test=1+2 3 C:\>set /a test=1+217522712576 Invalid number. Numbers are limited to 32-bits of precision. 
+5
source

Please note that the CMD has an accuracy of betweeb -2^31 to 2^31-1 which is -2 147 483 648 - 2 147 483 647 If the size is less than the limit, a warning came: Invalid number. Numbers are limited to 32-bits of precision. Invalid number. Numbers are limited to 32-bits of precision.

+4
source

As others said, the numbers are too large, however if you keep them as strings and outputs of the same length, it looks like

 @echo off rem cant do this get: Invalid number. Numbers are limited to 32-bits of precision. set Free=217522712576 set Need=2000000000 rem can do set Free=00000000000%Free%X set free=%Free:~-13% set Need=00000000000%Need%X set Need=%Need:~-13% echo Free=%Free% echo Need=%Need% echo on IF %Free% GTR %Need% (GOTO Sufficient_Space) ELSE GOTO Insufficient_Space @echo off :Insufficient_Space @ECHO INSUFFICIENT SPACE GOTO DONE :Sufficient_Space @ECHO SUFFICIENT SPACE :DONE 
+3
source

Source: https://habr.com/ru/post/899310/


All Articles