0

I need to concatenate a string after modifying an array in a windows batch script.

Can anyone point what am I doing wrong here?

For this I wrote batch script script_tests.bat. After executing the script script_tests.bat should be produce Tags="@TestCaseKey=2,@TestCaseKey=5"

Invoke script with parameters: script_tests.bat "testcaseIds=[2,5]"

:: script_tests.bat

:parse
IF "%~1"=="" GOTO endParse
ECHO "%~1" | FIND /I "=" && SET "%~1"
SHIFT /1
GOTO parse
:endParse

set testcaseIds=%testcaseIds:[=%
set testcaseIds=%testcaseIds:]=%

FOR %%A IN (%testcaseIds%) DO (
    set Tags=@TestCaseKey=%%A,%Tags%
)
echo %Tags%
Aruna
  • 701
  • 10
  • 26
  • `DelayedExpansion` is required with `!` expensaion to modify a variable during a for loop if it references itself or if the new value is referenced within the for loop. Alternately, `Call Set` can be used in conjunction with doubling `%` symbols of the variable to be referenced. IE: `Call set "Tags=@TestCaseKey=%%A,%%Tags%%"` – T3RR0R Aug 17 '21 at 06:39
  • `FOR /f "tokens=1,2 delims=[,]" %%A IN ("%testcaseids%") DO set Tags=@TestCaseKey=%%A,%%B` removes the need for delayed expansion *and* makes it unnecessary to remove the `[ ]`. – Stephan Aug 17 '21 at 07:35
  • Use this line instead of your FOR: `set Tags="@TestCaseKey=%testcaseIds:,=,@TestCaseKey=%"` This works with _any number_ of subscripts. Note: you are _not_ working with [arrays](https://stackoverflow.com/a/10167990/778560) here... – Aacini Aug 18 '21 at 16:21

0 Answers0