Below I'm testing a simple IF statement but it doesn't seem to take the whole number I'm comparing in to account. You can see this number is greater than "2", but with my result it's like it's only reading the "1" and that's it, so telling me the number is "Less or Equal" than "2", I would expect the result "LARGE" to be returned. How can I get it to read the whole number and not just the first digit?
IF "1073740972" LEQ "2" (
ECHO LESS or EQU 2
) ELSE (
ECHO LARGE
)
The "LARGE" number is an %ERRORLEVEL% generated number so this can be different each time.
I have tried Changing [LEQ "2"] to [LSS "3"] but I do not know what else I could try.
Thanks in advance
Remove the quotation marks from the comparison expressions:
IF 1073740972 LEQ 2 (
ECHO LESS or EQU 2
) ELSE (
ECHO LARGE
)
Otherwise, no numeric but string comparison is performed, where the character codes are regarded, so character 1 (ASCII 49 = 0x31) is less than character 2 (ASCII 50= 0x32).
Related
Here is my sample batch file code and I really don't know what it does.
set TEMPRPSW=%RPSW_VERSION%
set RELVER=%TEMPRPSW:~0,4%
set RELVER=%RELVER:.=%
if %RELVER% GEQ 30 goto :eof
Please give me a working sample.
That takes a 4 character long substring of TEMPRPSW, starting from character 0.
Meaning, it takes the first 4 characters of TEMPRPSW and puts them in RELVER.
set TEMPRPSW=abcdef
set RELVER=%TEMPRPSW:~0,4%
echo %RELVER% -> will print abcd
%VAR:str=% removes str
set RELVER=123.456
set RELVER=%RELVER:.=%
echo %RELVER% -> will print 123456 with no .
here is a nice article: https://www.dostips.com/DtTipsStringManipulation.php
This question already has an answer here:
Windows batch file IF failure - How can 30000000000000 equal 40000000000?
(1 answer)
Closed 5 years ago.
I have a very silly issue but unable to figure out what is wrong
I'm doing a basic level number comparison in if statement. script is pasted below. geq-greater than or equal to is working fine but gtr-greater than has a problem. Is there any limitation for numbers in gtr comparison?
if 3 gtr 2 (#echo "greater") else (#echo "lesser")
greater
if 3 geq 2 (#echo "greater") else (#echo "lesser")
greater
if 135149772801 gtr 111110000000 (#echo "greater") else (#echo "lesser")
lesser
if 135149772801 geq 111110000000 (#echo "greater") else (#echo "lesser")
greater
Is there any limitation for numbers in gtr comparison?
Yes, there really is.
You're limited to 32-bit signed integers and, if there's overflow, it's set to the maximum value.
Since both those numbers, 135149772801 and 111110000000, are too big, they're both converted to 231 - 1.
That's why it says the first is not greater (which is subtly different to lesser, by the way) but it is greater than or equal to.
More details can be found at the excellent dostips site.
A way around the 32-bit limit is to prefix the numeric-string with a goodly number of 0 and then use an alphabetic comparison on the last n characters.
Hence
set "num1=135149772801"
set "num2=111110000000"
set "zeroes=00000000000000000000000000000000000000000000000000000"
set "comp1=%zeroes%%num1%"
set "comp2=%zeroes%%num2%"
if "%comp1:~-20%" gtr "%comp2:~-20%" (echo greater) else (echo not greater)
where "-20" above means "take the last 20 characters"
A batch-file natively supports 32-bit signed integer arithmetic only. If the number goes over 2147483648, it overflows and warp back to negative number. However in if statements, the number gets clutter back to 2^31-1.
Code | Code Result | Actual Result
-----------------------+-------------------+-----------------------
2147483647 + 1 2147483648 2147483648
2147483648 + 1 -2147483648 2147483649
Here is some possible workaround:
Chopping the number(making the number smaller before comparing)
Other scripting languages(use powershell or vbs to compare)
Embedded Powershell solution:
for /f %p in ('powershell -command if (1 -gt 0^) {write-host greater} else {write-host lesser}') do echo %p
This command calls powershell to compare, and retrieve the results using a for loop.
I have two variables. var1 = 1.0 ; var2 = 0.9.
I want to do this:
set var1=1.0
set var2=0.9
if %var1%=%var2% echo equal
if %var1%(not equal to)%var2% echo not equal
pause
Does anybody know what look the "not equal" symbol should look like?
if "%var1%" neq "%var2%"
or
if not "%var1%"=="%var2%"
Type help if in a console window for more info. You should also get into the habit of enclosing both sides of the == signs in quotation marks, in case one of the variables contains a space.
if %var1% neq %var2% echo not equal
there's also geq gtr lss leq and equ (greater or equal, greater, less, less or equal and equal)
But - it's best to use "quotedvariablenames" on both sides of the comparison if you're using strings that contain separators (like spaces) and you can also use
if /i ...
to compare the strings case-insensitively
and also you can use
if not ....
to negate a condition
or
if condition (dothis) else (dothat)
There must be a separator before the opening parenthesis of the IF true-condition target
That opening parenthesis must be on the same physical line as the if
Where an 'else' clause is used, the ending parenthesis of the "true" block, a separator and the else keyword must be on the same physical line
Where an 'else' block is used, the else keyword, a separator and the opening parenthesis of the "else" block must be on the same physical line
ie
if condition (
something
) else (
someotherthing
)
but be careful!
batch does not support floating-point. comparing 1.0 with 0.9 will be performed as an alphabetic comparison and 2.0 will be reported as greater than 19.0 (easy with not-equal...)
using dos commands how can I match substring from a variable.
e.g: var="Lost = 0 (0% loss)"
I want to check whether var contains "Lost" in it.
I have already tried Contains, like but throwing error.
echo %varname%|findstr "string" >nul
if errorlevel 1 (echo %varname% does not contain "string"
) else (echo %varname% contains "string")
Note that this can all be on one line if you prefer. If broken across lines, the sequence ) else ( must be on same line, as must the if and very first (
In the following code, adding the same letter to both operands of the comparison changes the result. Despite - being not greater than j, -k is greater than jk.
This only happens if one of the operands is the minus sign (-) or single quotation mark (').
Why does this happen? What are the rules?
if - gtr j (echo - greater than j) else echo - less than j
if "-" gtr "j" (echo "-" greater than "j") else echo "-" less than "j"
echo.
if -k gtr jk (echo -k greater than jk) else echo -k less than jk
if "-k" gtr "jk" (echo "-k" greater than "jk") else echo "-k" less than "jk"
echo.
if ' gtr u (echo ' greater than u) else echo ' less than u
if "'" gtr "u" (echo "'" greater than "u") else echo "'" less than "u"
echo.
if 'v gtr uv (echo 'v greater than uv) else echo 'v less than uv
if "'v" gtr "uv" (echo "'v" greater than "uv") else echo "'v" less than "uv"
The result is:
- less than j
"-" less than "j"
-k greater than jk
"-k" greater than "jk"
' less than u
"'" less than "u"
'v greater than uv
"'v" greater than "uv"
You may be assuming that strings are just compared character by character, taking their ordinal values.
That's not true. Collation is much more complex than that.
In fact, you can see the same in other environments, such as Windows PowerShell:
PS Home:\> '-' -gt 'j'
False
PS Home:\> '-k' -gt 'jk'
True
PS Home:\> '''' -gt 'u'
False
PS Home:\> '''v' -gt 'uv'
True
It could very well be that the order of strings varies with your locale as well.
As for your particular problem here, quoting from the Unicode Collation Algorithm (UTS #10):
Collation order is not preserved under concatenation or substring operations, in general.
For example, the fact that x is less than y does not mean that x + z is less than y + z, because characters may form contractions across the substring or concatenation boundaries. In summary:
x < y does not imply that xz < yz
x < y does not imply that zx < zy
xz < yz does not imply that x < y
zx < zy does not imply that x < y
and to solve the misconveption you're likely under:
Collation is not code point (binary) order.
A simple example of this is the fact that capital Z comes before lowercase a in the code charts. As noted earlier, beginners may complain that a particular Unicode character is “not in the right place in the code chart.” That is a misunderstanding of the role of the character encoding in collation. While the Unicode Standard does not gratuitously place characters such that the binary ordering is odd, the only way to get the linguistically-correct order is to use a language-sensitive collation, not a binary ordering.