Solved

Batch File Compare numbers issue

Posted on 2015-01-07
4
226 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
  • 2
4 Comments
 
LVL 23

Expert Comment

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

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now