1

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)

Zap Sira
  • 33
  • 5
  • 5
    I'd start by changing `set str1=%3` to `Set "str1=%~3"`, and `if not "%~3"=="" (` to `If Defined str1 (`. As for your main command, I'd advise you to [edit] your question to explain what it is supposed to be doing, with an actual example we can use to reproduce your intention and issue. I'd assume it would look better like this though `If /I Not "x%str1:TEST=%" == "x%str1%" Set "additional_config=UNIT_TEST SKIP_WAIT %str1%"`, if you intend for it to assign the variable only if the case insensitive string `TEST` was included as part of the initial third argument. – Compo Apr 26 '21 at 16:38
  • 2
    Always a best practice to use quotes for just about everything which includes defining variables, using file paths and string comparisons because spaces are a command separator. `if not "x%str1:TEST=%"=="x%str1%"` – Squashman Apr 26 '21 at 16:45
  • 3
    And with quotes, you don't need the `x` anymore, just `if not "%str1:TEST=%" == "%str1%"` – jeb Apr 27 '21 at 05:36
  • 1
    I recommend to read my long answer on [What is the difference between “…” and x“…” in an IF condition in a Windows batch file?](https://stackoverflow.com/a/52418905/3074564) and the other posts referenced by this answer. Then you have deep knowledge on what happens on execution of the posted command lines, why they fail, what is better and why. See also __issue 2__ (and the other issue chapters) of [this answer](https://stackoverflow.com/a/60686543/3074564). – Mofi Apr 27 '21 at 06:20

1 Answers1

4

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
)
jeb
  • 78,592
  • 17
  • 171
  • 225