Reputation: 331
The following code takes in all command line parameters of a batch file. In my case I have about 30 command line parameters and they are all numbers of 1, 2, or 3. I take them in then want to reassign them to other characters. I want each var, if it is a 1, change it to /*, if it's a 2, change it to */, if it's a 3, change it to #.
The first part works great, it's the second part of reassigning that I can't get the syntax for.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set var!count!=%%i
set var!count!=!var!count!:1=/*! <--don't work
set var!count!=!var!count!:2=*/! <--don't work
set var!count!=!var!count!:3=#! <--don't work
set /a count=!count!+1
)
Upvotes: 3
Views: 138
Reputation: 130819
I think jeb has the simplest solution, but there are other options.
1) You can transfer the current value of count into a FOR variable. This is how I tend to do it.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
for %%N in (!count!) do (
set "var%%N=%%i"
set "var%%N=!var%%N:1=/*!"
set "var%%N=!var%%N:2=*/!"
set "var%%N=!var%%N:3=#!"
)
set /a count+=1
)
2) You can use CALL to delay expansion of the outer variable, but I don't like this option because the normal expansion is not as safe as delayed expansion.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set "var!count!=%%i"
call set "var!count!=%%var!count!:1=/*%%"
call set "var!count!=%%var!count!:2=*/%%"
call set "var!count!=%%var!count!:3=#%%"
set /a count+=1
)
Upvotes: 2
Reputation: 82217
The problem is the way of accessing the array members.
In your case, the best way seems to use a temp variable.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set "temp=%%i"
set "temp=!temp:1=/*!"
set "temp=!temp:2=*/!"
set "temp=!temp:3=#!"
set "var!count!=!temp!"
set /a count+=1
)
Upvotes: 2