Link to home
Start Free TrialLog in
Avatar of PAH_in_MI
PAH_in_MIFlag for United States of America

asked on

Batch File Compare numbers issue

I am having issues with a variable compare, and I am just not seeing the resolution.

I have an input file called 'check_out.txt" that looks like this:
<there is an empty line here>
+----------------------+
|REC_PER_SEC           |
+----------------------+
|100.00                     |
+----------------------+

Open in new window


My script is to go to the 5th line, read the total, strip off the spaces and pipe, and then make certain the number is greater than a fixed number.

My code is:
@echo off
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%a in (`more +%lineNr%  c:\temp\check_out.txt`) DO (
  set xxx=%%a
  goto :getnumber
)

:getnumber
set myresults1=%xxx:|=%
set myresults=%myresults1: =%
echo %myresults%
  goto :comparenumber
  
:comparenumber
if "%myresults%" GTR "40.00" (
  echo "Above threadhold of 40.00 records per second. All is good"
  goto :GOODEND
) else (
  echo "Below threshold of 40.00 records per second. Please investigate"
  echo "Number of Records per second: %myresults%"
  goto :BADEND
)

:GOODEND
REM del c:\imd_check\imd_check_out.txt
exit /B 0

:BADEND
REM del c:\imd_check\imd_check_out.txt
exit /B 99

Open in new window


No matter how I run this---it always returns:

100.00
"Below threshold of 40.00 records per second. Please investigate"
"Number of Records per second: 100.00"

What am I missing?
Avatar of NVIT
NVIT
Flag of United States of America image

You can't have a decimal point. Also, remove the quotes. i.e. 100 gtr 40
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PAH_in_MI

ASKER

Thanks for the solution.  I guess I just assumed decimals were allowed. I am giving you full credit because not only did you state that, but you also provided code to resolve it---and made the remove of spaces a bit cleaner.

Thanks
Avatar of oBdA
oBdA

Well, decimals are basically "allowed", but since the shell finds a non-numeric character, it will do a string comparison instead of a numeric one, and then "100" will indeed be less than "40".
The shell will do a string comparison as well if you enclose the values in quotes.
And the 32bit limit can really lead to unexpected results, because 2147483648 will be treated as -1 ...
Just for fun:
@echo off
call :Test 10 gtr 2
call :Test 10.0 gtr 2
call :Test "10" gtr "2"
call :Test 2147483647 gtr 2147483646
call :Test 2147483648 gtr 2147483647
call :Test "2147483648" gtr "2147483647"
goto :eof

:Test
echo Testing '%*' ...
if %* (echo ... true) else (echo ... false)
goto :eof

Open in new window