Solved

Batch File Compare numbers issue

Posted on 2015-01-07
4
378 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 85

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 85

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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