For loop in Batch script, Delayed Expansion is broken

antonio altamura 106 Reputation points

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

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

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/*"


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.
281 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. MotoX80 29,371 Reputation points

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


    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