Solved

Batch File Compare numbers issue

Posted on 2015-01-07
4
211 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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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 …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

15 Experts available now in Live!

Get 1:1 Help Now