'Im trying something with IF in Batch but is not working as im expecting

I need to do an script that looks for some text in a file and also look for a file itself in a %userprofile% path, and it works fine but when i tried to unify it so that instead of outputing 2 confirming messages only output 1, something is not going as expected, this is the code:

hostname > hstnm.txt
SET /p hstnm=<hstnm.txt
SET pccer=.filextension1
SET pcsip=.filextension2
SET fullhost=%hstnm%%pccer%
SET fullsip=%hstnm%%pcsip%
SET fullroute=%userprofile%\thepath\%fullhost%
SET siproute=%userprofile%\thepath\%fullsip%

FINDSTR /m "<Protocol>TLS</Protocol>" "%siproute%"
IF %errorlevel%==0 (
 SET siptest="true"
) ELSE (
 SET siptest="false"
)

IF EXIST "%fullroute%" (
 SET certest="true"
) ELSE (
 SET certest="false"
)

IF %siptest%=="true" & %certest%=="true" (
ECHO message if everything good
) ELSE (
ECHO message if something bad
)
pause

The FINDSTR and the IF EXIST are working properly (if u swap SET for an ECHO it does display a message). The problem arrives at the part of using the %certest% and the %siproute%, it is doing nothing, just outputs this (if i quit the @echo off)

...
C:\path>IF EXIST "C:\Users\Administrador\Appdata\Roaming\Interactive Intelligence\PureCloud Softphone\certificates\sip\GSSES0401107C.grupogss.corp.cer" (SET certest="true" )  ELSE (SET certest="false" )
No se esperaba & en este momento.
C:\path>IF "true"=="true" & "true"=="true" (
C:\path>

Help me please! thanks in advance.



Solution 1:[1]

The line in question has a '&' character which I assume you are trying to use as a boolean AND operator - 'IF' in CMD does not have an AND operator. The CMD command chain mechanism does and that's where the probably confusion arises. I typically nest IFs in cases like this:

IF %siptest%=="true" (  
    IF %certest%=="true" (...

The '&' operator " simply chains a number of command together on one line:

dir & cd \ & dir & del myfile.txt

All the above command will be executed, one after the other.

On the other hand, the '&&' operator works a bit more like an AND operator in that it executes the next command in the chain only if ERRORLEVEL of the previous command is "0" (no error).

del myfile.txt && echo Done! && cd c:\

The echo command will only be executed if del command returns a 0 as its ERRORLEVEL. The cd command is dependent on the results all of the previous commands, meaning it only takes one failed command to break the chain.

I am personally also careful how I use variables and always enclose them:

IF "%MYVAR%"=="0" (...

I do that because in the end you are not comparing a variable to something, but rather it is a simple string comparison; CMD variables are replaced with the content of what they represent before any actions on them. When that replacement happens can be adjusted to a certain degree, but in the end it is always a string comparison. That means that if the variable doesn't have any content (it happens) and you use that to compare, without some kind of enclosure, then your script will fail and exit, due to the resulting IF being malformed.

https://ss64.com/nt/if.html
https://ss64.com/nt/syntax-conditional.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1