separating variable with a symbol into different parts

16,176

Solution 1

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

Solution 2

The best way to split a variable into an array (or as close to an array as Windows batch can imitate) is to put the variable's value into a format that can be understood by a for loop. for without any switches will split a line word-by-word, splitting at csv-type delimiters (comma, space, tab or semicolon).

This is more appropriate than for /f, which loops line-by-line rather than word-by-word, and it allows splitting a string of an unknown number of elements.

Here's basically how splitting with a for loop works.

setlocal enabledelayedexpansion
set idx=0
for %%I in ("%var:;=","%") do (
    set "var[!idx!]=%%~I"
    set /a "idx+=1"
)

The important part is the substitution of ; into "," in %var%, and enclosing the whole thing in quotation marks. Indeed, this is the most graceful method of splitting the %PATH% environment variable for example.

Here's a more complete demonstration, calling a subroutine to split a variable.

@echo off
setlocal enabledelayedexpansion

set string=one;two;three;four;five;

:: Uncomment this line to split %PATH%
:: set string=%PATH%

call :split "%string%" ";" array

:: Loop through the resulting array
for /L %%I in (0, 1, %array.ubound%) do (
    echo array[%%I] = !array[%%I]!
)

:: end main script
goto :EOF


:: split subroutine
:split <string_to_split> <split_delimiter> <array_to_populate>
:: populates <array_to_populate>
:: creates arrayname.length (number of elements in array)
:: creates arrayname.ubound (upper index of array)

set "_data=%~1"

:: replace delimiter with " " and enclose in quotes
set _data="!_data:%~2=" "!"

:: remove empty "" (comment this out if you need to keep empty elements)
set "_data=%_data:""=%"

:: initialize array.length=0, array.ubound=-1
set /a "%~3.length=0, %~3.ubound=-1"

for %%I in (%_data%) do (
    set "%~3[!%~3.length!]=%%~I"
    set /a "%~3.length+=1, %~3.ubound+=1"
)
goto :EOF

Here's the output of the above script:

C:\Users\me\Desktop>test.bat
array[0] = one
array[1] = two
array[2] = three
array[3] = four
array[4] = five

Just for fun, try un-commenting the set string=%PATH% line and let the good times roll.

Share:
16,176
user2035296
Author by

user2035296

Updated on June 27, 2022

Comments

  • user2035296
    user2035296 almost 2 years

    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