Check if there's a difference between two variables - batch-file

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...)

Related

=str13 was unexpected at this time batch file

I'm just unable to make it work.
I used another solution to determine if a received argument contains a certain substring. But it fails whenever I don't enter a third argument. I tried 4/5 methods to check if it exists and to condition the search within the variable but it doesn't seem to work!
set str1=%3
if not "%~3"=="" (
if not x%str1:TEST=%==x%str1% SET additional_config=UNIT_TEST SKIP_WAIT %3
)
How can I avoid evaluating %3 if it doesn't exist to avoid the error?
The code simply should set additional_config if 'TEST' is part of the third argument received. (if there is a third argument)
Your problem is the parsing of if not x%str1:TEST=% or even if not "%str1:TEST=%".
This part is parsed, even if %3 is empty and there comes the problem.
str1 is empty in that case. Percent expansion with search/replace of an empty/undefined variable has unexpected results.
In your case the parser detects at the stage %str1:, that str1 is undefined and stops the percent expansion.
Therefore, the parser sees TEST=%==x%str1% SET additional_config=UNIT_TEST SKIP_WAIT %3 and it splits it at pairing percent signs:
TEST= %==x% str1 % SET additional_config=UNIT_TEST SKIP_WAIT % 3
The parts in percent signs are invalid or undefined variables
TEST= ... str1 ... 3
At this point the program stops with a syntax error, because the block (inside parenthesis) contains an invalid IF statement
How to solve it?
One way is to store the value before
set "str1=%~3"
set "contains=%str1:TEST=%"
if not "%~3"=="" (
if not "%contains%" == "%str1%" SET additional_config=UNIT_TEST SKIP_WAIT %3
)
Or even better, use delayed expansion, that doesn't fail this way
#echo off
setlocal EnableDelayedExpansion
set "str1=%~3"
if not "%~3"=="" (
if not "!str1:TEST=!" == "!str1!" SET additional_config=UNIT_TEST SKIP_WAIT %3
)

Simple IF statement with more than 1 digit numbers?

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).

What does "!S:~%I%,1!"=="" mean?

I found some sample code but I am unable to get what this if condition means:
set /p sourceDB=Enter Source DB: %=%
set S=%sourceDB%
set I=0
set L=-1
:l ----- Forget about this line
if "!S:~%I%,1!"=="" goto ld
if "!S:~%I%,1!"=="/" set K=%I%
if "!S:~%I%,1!"=="#" set Z=%I%
if "!S:~%I%,1!"==":" set Y=%I%
set /a I+=1
goto l
The short answer is that this is how you get substrings in batch.
When you extract a substring, you use the format %string_name:~index_of_first_character_in_substring,length_of_substring% or, if the value of either index_of_first_character_in_substring or length_of_substring is contained in a separate variable (in your example, the index is its own variable), you can enable delayed expansion and use the format !string_name:~%variable_whose_value_is_the_index_of_first_character_in_substring%,length_of_substring!
In this case, your main string is in a variable called %S%, you are starting at character %I%, and grabbing 1 character.
The line you've told us to ignore is actually pretty important, as it's used to loop through the entire string.
The entire line "!S:~%I%,1!"=="" is used to check if the substring is empty -- that is, the script is finished iterating through the string. There are also conditions for if the substring is /, #, and :; with K, Z, and Y respectively containing the indices of those substrings.

Error in Batch file when 2nd parameter is not supplied

Here is a batch file, I need to set the second parameter to a certain path if it is not supplied, else use the value of second parameter further. Note that i need to access the value of EXECUTE_DIR, further in the file
dummy.bat
IF %2 == "" (
SET EXECUTE_DIR = "c:\Program Files"
) ELSE (
SET EXECUTE_DIR = %2
)
ECHO exedir = %EXECUTE_DIR%
-
when I provide only 1 parameter, I get following output:
D:>dummy.bat "Lab"
( was unexpected at this time.
D:>IF == "" (
D:>
you need
if "%2"=="" ....
both sides of the comparison operator must exactly match. If %2 does not exist, your code is resolved to
if =="" ...
which is clearly a syntax error as reported.
My preferred version is
set "var=%~2"
if not defined var ...
which conveniently assigns the value of %2, with enclosing quotes removed, to var.
naturally, you could use if defined var... if that's more convenient.
meanwhile, your )else( will also generate an error. You must have spaces both sides of the else, otherwise cmd doesn't know whether you are invoking an else clause or )else( is some variety of variable or option or whatever.
The ) and ( must also be on the same physical line as the else keyword (which you have).
(belay that - proportional-space ib unformatted text makes the spaces hard to spot...)
But that brings up another point - Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN". The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.

using dos commands how can I match a substring from a variable?

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 (

Resources