separating variable with a symbol into different parts

batch-file, string

Solution

`Tokens=1,2` does create the two for loop variables `%%i` and `%%j`& splits `string` in two parts, separated by the delimiter `;`:

@echo off &setlocal
set "string=var1;var2;"
for /f "tokens=1,2 delims=;" %%i in ("%string%") do set "variable1=%%i" &set "variable2=%%j"
echo variable1: %variable1%
echo variable2: %variable2%
endlocal
pause

For a more "dynamic" method use this:

@echo off &setlocal enabledelayedexpansion
set "string=var1;var2;"

set /a count=0
for %%i in (%string%) do (
    set /a count+=1
    set "variable!count!=%%i"
)
echo found %count% variables
for /l %%i in (1,1,%count%) do (
    echo variable%%i: !variable%%i!
)
endlocal

Problem

using batch, i want to be able to separate a variable into two or three parts, when there is a symbol dividing them. for example if i have the string which looks like this: var1;var2; how can i get var1 to become variable and var2 to become a different one. Thanks in Advance

Original source

Related problems