question

antonioaltamura-7023 avatar image
0 Votes"
antonioaltamura-7023 asked IanXue-MSFT edited

For loop in Batch script, Delayed Expansion is broken

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

antonioaltamura-7023 avatar image
0 Votes"
antonioaltamura-7023 answered

Thanks, apparently it was just that I missed call before launching cypress.

 call npx cypress run
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

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?


capture.jpg (129.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.