?
Solved

Batch File Compare numbers issue

Posted on 2015-01-07
4
Medium Priority
?
407 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 25

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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

800 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