For loop in Batch script, Delayed Expansion is broken

antonio altamura 106 Reputation points
2021-06-08T12:41:26.107+00:00

Delayed expansion acts weird in my for loop. At the first iteration study is defined, at the second iteration it seems the delayed expansion doesn't work (delayed expansion is used just to strip the colon out because that will be the name of a directory). It is like that cypress command breaks something. Is that somehow related to how that cypress CLI works or there is something wrong with my script?

@echo off
setlocal EnableExtensions EnableDelayedExpansion

:parse
IF "%~1"=="" GOTO endparse
IF "%~1"=="-s" SET studies=%~2
SHIFT
GOTO parse
:endparse


for %%a in ("%studies:,=" "%") do (
set tmp=%%~na
set study=!tmp::=!
echo study %%~a delayedExString: !study!
npx cypress run -study=%%~a --config videosFolder=cypress/videos/!study! --headless --spec "cypress/integration/*"
)

Example:

myscript.bat -s "firstStudy:Chap1,secondStudy:Chap3"

Console output:

study firstStudy:Chap1 delayedExString firstStudyChap1
[...cypress log...]


study firstStudy:Chap1 delayedExString !study!
[...cypress log...]

As you can see, in the second iteration study is not expanded.

PS: The tags are kind of wrong as there is no "batch" tag. Please note the script runs on a plain Windows 10 machine.

Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
296 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. MotoX80 31,391 Reputation points
    2021-06-08T13:25:57.61+00:00

    I changed the npx command to just echo what it would execute instead of actually executing the command.

    103410-capture.jpg

    It appears to be executing the commands that you expect to be executed. So your thought that "the cypress command breaks something" appears correct.

    Unfortunately, npx is not a Windows 10 command. I have no idea what npx/cypress is or does. If npx is another .bat file, you could try using a CALL statement.

    Have you considered using Powershell instead of a .bat?

    0 comments No comments