2
SET CLIENTS=blah1:blah2

for %%x in (%CLIENTS::= %) do (
SET client=%%x
echo %client%;
if "%1"==%client% goto end
)
:end

I would expect to compare blah1 and %1 first and blah2 and %2 second. However, the %client% will only be assigned with blah2. Any ideas how to resolve this issue?

1 Answer 1

3

The expansion of variables inside FOR loops requires you to enable delayed expansion to force variables to be expanded at runtime instead of being expanded when parsed. Read HELP SET for more information.

And try changing your code to

@echo off
setlocal enabledelayedexpansion
SET CLIENTS=blah1:blah2
set CLIENTS=%CLIENTS::= %
for %%x in (%CLIENTS%) do (
  SET client=%%x
  echo !client!;
  if "%1"=="!client!" goto end
  )
:end

Note that the variable is referenced with an slightly different syntax !client! instead of %client%. Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time.

Sign up to request clarification or add additional context in comments.

2 Comments

I used the code above, however, when I provided blah1 as arg1, it still couldn't match the condition.
@VictorW - that is because the left side of the comparison is quoted, and the right side is not. The line should read if "%~1"=="!client!" goto end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.