Solved

Batch File Compare numbers issue

Posted on 2015-01-07
4
333 Views
Last Modified: 2015-01-07
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?
0
Comment
Question by:PAH_in_MI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40535764
You can't have a decimal point. Also, remove the quotes. i.e. 100 gtr 40
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40535791
Here's an adjusted version that simply ignores everything after the decimal point (if you really need it, please tell us the expected maximum value for the records per second); note that Batch can only calculate with integers from -2147483648 to 2147483647.
@echo off
set CheckOutFile=C:\temp\check_out.txt
set Threshold=40
set lineNr=5
set /a Skip = lineNr - 1
for /f "skip=%Skip% delims=|. " %%a in ('type "%CheckOutFile%"') do (
  set Rec_Per_Sec=%%a
  goto :CompareNumber
)

:CompareNumber
if %Rec_Per_Sec% GTR %Threshold% (
  echo Above threshold of %Threshold%.00 records per second. All is good.
  goto :GOODEND
) else (
  echo Below threshold of %Threshold%.00 records per second. Please investigate.
  echo Number of Records per second: %Rec_Per_Sec%.00
  goto :BADEND
)

:GOODEND
REM del "%CheckOutFile%"
exit /B 0

:BADEND
REM del "%CheckOutFile%"
exit /B 99

Open in new window

Note that for the "below" message to be technically correct, you'd need to use GEQ instead of GTR in the comparison.

Edit: corrected test threshold of 110 back to 40.
0
 

Author Closing Comment

by:PAH_in_MI
ID: 40535835
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
0
 
LVL 84

Expert Comment

by:oBdA
ID: 40535895
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

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question