1

I have the following string:

AAA:BBB, CCC:DDD, EEE:FFF

I need to extract BBB, DDD and FFF into separate variables, because I need to run another script once for each of these variable, with the variables as parameters. In this example I would need to run:

script.cmd BBB
script.cmd DDD
script.cmd FFF

The string could be just "AAA:BBB" or it could have 10 of these formats, up to "YYY:ZZZ". My train of thoughts is to make an array out of the string. First I will remove all eventual spaces in the initial string, then separate the elements on comas. So the result would be:

Array[0]=AAA:BBB
Array[1]=CCC:DDD
.......
Array[n]=YYY:ZZZ

After that, I can take each element and try to delete "*:", and be left with :

Array[0]=BBB
Array[1]=DDD
.......
Array[n]=ZZZ

And then I can use these as parameters. I did the following:

rem -- set string
set STRING=AAA:BBB, CCC:DDD, EEE:FFF

rem -- trim eventual spaces in STRING
set STRING=%STRING: =%

rem -- replaces comas with spaces to be able to create array
set STRING=%STRING:,= %

setlocal ENABLEDELAYEDEXPANSION

rem -- split string into array
set index=0
for %%A in (%STRING%) do (
  set Array[!index!] = %%A
  set /a index += 1
  )

Splitting works great. But this is where I got stuck. I tried for the past hours to take each element of the array and remove all characters until colon, including colon. Not sure what I'm doing wrong, the output is just weird. I tried using the same for, used for splitting the string, to also run the scripts with parameters. I tried using another for after the splitting is complete. Nothing works.

Seems like the command:

set string=%string:*:=%

.. does not work for some reason for each element of the array. Output is weird, something like "stringn=%=%" . And I don't understand why. Same command works great for other variables that are not elements of the array.

Anyone have any suggestions? Much appreciated.

2 Answers 2

2

Here's another method:

@echo off
set "string=AAA:BBB, CCC:DDD, EEE:FFF"
for %%a in (%string::=\%) do echo script.cmd %%~nxa
Sign up to request clarification or add additional context in comments.

3 Comments

Great idea to handle it like a file.
Thank you. Works great. Mind explaining it a bit so I can understand it? You are replacing all colons with \ , what does %%~nxa do ?
%%~nxa returns the filename and extension of the a metavariable from a path\filename string. When changing the : to \ it makes it look like a path\filename so it returns the part after the last \ character in each term. Spaces and some other characters will break this method, but you didn't indicate the character set being used in your terms so we have to go by the terms you provide. The for command eats the commas in this usage.
2
set "string=AAA:BBB, CCC:DDD, EEE:FFF"
for %%i in (%string%) do for /f "tokens=2 delims=:" %%j in ('echo %%i') do echo script.cmd %%j

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.