1

This is my script:

@echo off
setlocal EnableDelayedExpansion
set "memuc=d:\file.exe"

set array[0]=foo
set array[1]=bar

set x=0


:SymLoop
if defined array[!x!] (
    call "!memuc!" start -n !!array[%x%]!!

    set /A x=!x! + 1
    GOTO :SymLoop
)

endlocal
  1. Why array[!x!] line cannot be replaced with with !array[%x%]!? I thought I have to use ! when accesing delayedExpansion variables.
  2. Why I have to use %x% instead !x!?. The same, I thought ! should be necessary accesing x variable
  3. Why I have to use two ! here !!array[%x%]!!?

1 Answer 1

1

I think you got the concept wrong. The ! means that the value of the variable will be expanded as late as possible.

@echo off
SETLOCAL EnableDelayedExpansion
set "_var=stackoverflow"
set "_var=rules" & echo "%_var% !_var!"

This will print "stackoverflow rules" due to the fact of the delayed expansion.

Now to your questions:

Addum 1. The reason is the if defined. If you would like to put there the actual variable you would use e.g. your array[0] that is the reason why you have array[!x!] and not !array[%x%]!.

Addum 2. You have to use %x% to have immediate expansion as you want to have array[0] and array[1]. If you would write it as !x! then you would be searching, if written as %array[!x!]% for array[!x!].

Addum 3. The double exclamation marks (!!) is there due to the fact that call introduces one more expansion. So first you have immediate expansion of %x% then you have an expansion of array[0] and then expansion for the call.

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

Comments

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.