Mads Gadeberg
Mads Gadeberg

Reputation: 1480

How to seperate a string with only '=' in windows batch script

I am having a problem with splitting a string into 2 different strings seperated by the '=' char. The For command is default seperating by spaces ' '. How can i make it not do that?

Code example

for /f "tokens=1,2 delims= = NOT SPACE" %%a in ("%~1") do set argument=%%a & set value=%%b

The string i would like to seperate:

ASentence = This is a sentence

What i get from above is ASentence and This

Upvotes: 0

Views: 39

Answers (1)

Magoo
Magoo

Reputation: 80203

for /f "tokens=1,* delims==" %%a in ("%~1") do set argument=%%a & set value=%%b

Note : the trailing space in argument (%%a) and leading in value (%%b) can be removed by using

set "argument=%argument:~0,-1%"
set "value=%value:~1%"

Note: tokens=1,2 would also work in this case if the value assigned to value (%%b) does not contain =.

Upvotes: 3

Related Questions