The following is based on JBEs answer in this thread. It distinguishes empty/undefined variables.
if not defined VARIABLE1 (echo VARIABLE1 undefined) & goto proceed
if not defined VARIABLE2 (echo VARIABLE2 undefined) & goto proceed
echo %VARIABLE1% | find "%VARIABLE2%" > nul
if ERRORLEVEL 1 (echo Not found) & goto proceed
echo Found
:proceed
If the value of VARIABLE1
contains parentheses, e.g. the value C:\Program Files (x86)
, then the parentheses may be interpreted as a distinct command rather than echoed, causing an error. Substrings like (x86)
can be escaped with carets for purposes of echoing properly, e.g.:
SET V1_DISPLAY=%VARIABLE1%
if /i "%V1_DISPLAY:(x86)=%" NEQ "%V1_DISPLAY%" set V1_DISPLAY=%V1_DISPLAY:(x86)=^^(x86^^)%
echo %V1_DISPLAY% | find "%VARIABLE2%" > nul
The second statement, above, can be read as follows:
If replacing substring (x86)
with nothing makes a difference, then (x86)
is present, so replace any occurrence of (x86)
with ^^(x86^^)
, where each pair of carets represent an escaped single caret.