3

I am trying to split data_set_path first by , than In side loop I should be able to split string by ->.Please help me.

set "data_set_path=v1->E:\test1,v2->E:\test2,v3->E:\test3,v4->E:\test4"

2 Answers 2

3

try this:

@echo off&setlocal 
set "data_set_path=v1->E:\test1,v2->E:\test2,v3->E:\test3,v4->E:\test4"
REM first split by commas
for /f "tokens=1-4 delims=," %%i in ("%data_set_path%") do set "pc1=%%i"& set "pc2=%%j"& set "pc3=%%k"&set "pc4=%%l"
<nul set/p"=1st split: %pc1% %pc2% %pc2% %pc4%"&echo( 
REM second split by "->"
for /f "tokens=1-4 delims=->" %%i in ("%data_set_path%") do set "pp1=%%i"& set "pp2=%%j"& set "pp3=%%k"&set "pp4=%%l"
echo 2nd split: %pp1% %pp2% %pp2% %pp4%
REM third split by "->"
for /f "tokens=1,2 delims=->" %%a in ("%pc1%") do set "pc11=%%a"&set "pc12=%%b"
for /f "tokens=1,2 delims=->" %%a in ("%pc2%") do set "pc21=%%a"&set "pc22=%%b"
for /f "tokens=1,2 delims=->" %%a in ("%pc3%") do set "pc31=%%a"&set "pc32=%%b"
for /f "tokens=1,2 delims=->" %%a in ("%pc4%") do set "pc41=%%a"&set "pc42=%%b"
echo 3nd split: %pc11% %pc12% %pc21% %pc22% %pc31% %pc32% %pc41% %pc42%

.. output is:

1st split: v1->E:\test1 v2->E:\test2 v2->E:\test2 v4->E:\test4
2nd split: v1 E:\test1,v2 E:\test1,v2 E:\test3,v4
3nd split: v1 E:\test1 v2 E:\test2 v3 E:\test3 v4 E:\test4

Sign up to request clarification or add additional context in comments.

2 Comments

Endoro I need to split string first by comma .Than on each string inside loop(say my first element is v1->E:\test1) I have to again split that means I should have v1 and E:\test1 as a separate value.
The first split is by comma and the 3th is the split of the first splitted now by "->". So forget the 2nd split I made because I was not sure what you need.
2

Use variable expansion search and replace to convert delimiter into line feed. FOR /F iterates at line feeds.

@echo off
setlocal enableDelayedExpansion

:: Define LF to contain a line feed (0x0A)
set LF=^


:: Above 2 empty lines are critical - DO NOT REMOVE

set "data_set_path=v1->E:\test1,v2->E:\test2,v3->E:\test3,v4->E:\test4"
for %%L in ("!LF!") do (
  for /f "delims=" %%A in ("!data_set_path:,=%%~L!") do (
    set "split1=%%A"
    set "split2a="
    set "split2b="
    for /f "delims=" %%B in ("!split1:->=%%~L!") do (
      if not defined split2a (set "split2a=%%B") else set "split2b=%%B"
    )
    echo name=!split2a!  value=!split2b!
  )
)

--OUTPUT--

name=v1  value=E:\test1
name=v2  value=E:\test2
name=v3  value=E:\test3
name=v4  value=E:\test4

The code above fails if name or value begins with ; due to FOR /F EOL feature.

The code also fails if ! appears anywhere within content due to delayed expansion being active when FOR variable is expanded.

Both problems can be solved if need be.

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.