Comparing Two Numbers
Building on the padding technique described in the Real Math article, it was simple to construct the following routine to compare the relative values of two numbers (or strings).

  A New and Improved Compare

Once the numbers are assured of being of equal length, it's a simple matter of sorting them to figure out which is larger. That was the idea behind my previous approach to comparison (see my User to User submission, Vol 15, #1, January 96 (no longer available online)). However, I think this new approach, using the DIR command is faster and more portable.
:: COMPARE.BAT - :: A 'greater than/less than' routine :: Modified to make it compatible with Win 2000, 25 Jan 2003 :: Thanks to Mark Lidemann for the recommendations :: Tom Lavedas <tlavedas@hotmail.com> :: http:://www.pressroom.com/~tglbatch/ :: @echo %dbga% off %4 if [%3]==[] for %%v in (echo goto:Exit) do %%v Syntax: %0 VarName Num1 Num2 if [%2]==[%3] %0 %1 %2 %3 goto:End E for %%v in ( *. ) do ren %%v *.!@# for %%v in (!*.bat) do ren %%v *.#@! &gt; !%2.bat echo.%0 %1 %2 %3 goto:End L &gt; !%3.bat echo.%0 %1 %3 %2 goto:End G for %%v in (!?.bat !??.bat !???.bat !????.bat !?????.bat !??????.bat) do ren %%v !%%v &gt; {t}.bat dir !*.bat /o/b %; Sorts the input strings with the smaller first ;% {t}.bat :End for %%v in (!*.bat {t}.bat?) do del %%v for %%v in ( *.!@#) do ren %%v *. for %%v in (!*.#@!) do ren %%v *.bat set %1=%5 %; For example ;% if [%2]==[%3] echo The numbers are equal: %1=%5 %; For example ;% if not [%2]==[%3] echo The smaller number is %2: %1=%5 %; For example ;% if not [%2]==[%3] echo The larger number is %3: %1=%5 :Exit ::

There is really little reason to use this procedure in Win NT/2000/XP, because those OS's already support this type of comparison in their IF statements (type IF/? at a command prompt for more information - or use the F1 help and search for "command reference" then select 'IF' from the list). The only reason I can think to use this approach with the NT related OS's is to provide a universal appoach for a mixed OS environment.
  Some Caveats

This procedure is designed primarily for the comparison of numbers, but strings can also be compared. However, it must be understood that the strings cannot contain control characters (those ASCII codes less than 32 decimal [space]) and that strings of unequal length will be right justified using explanation points (!) as the padding character. This is not generally a useful comparison for strings. It is usually more helpful to compare strings that are not padded or that are left justified. However, this approach is indeed useful for strings representing hexidecimal numbers.

As constructed, the routine assumes the length of the number strings to be eight or fewer characters. Further, numbers with leading zeros can be compared, only if both numbers are of the same length before being input to COMPARE.BAT. However, this restriction can be removed for numbers by replacing the explanation point padding characters in the routine above with zeros.




Top | General Notes | Homepage