0

I need to split a string that come from a variable and, according to another variable, print the result, something like the following example set "DIRECTORY=c:\test1;c:\test2"

set "PROG=TEST1"
REM "PROG=TEST2"

for /f "tokens=* delims=;" %%f in ("%DIRECTORY%") do (
if "%PROG%" == "TEST1"
    echo ...... c:\test1
) else (
    echo ...... c:\test2
)
)
Pedro Sales
  • 513
  • 2
  • 8
  • 24
  • possible duplicate of [How to split a string in a Windows batch file?](http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file) – sclarke81 Aug 19 '14 at 08:55
  • Almost ...the answer to that post are based by the tokens ...what I need is like a loop, independent of the tokens – Pedro Sales Aug 19 '14 at 09:15
  • You're right, sorry, it is a duplicated ...thank you – Pedro Sales Aug 19 '14 at 09:30
  • No problem, I wasn't sure to begin with, but there are so many solutions in the other question that it looked like one of them would hit the mark. – sclarke81 Aug 19 '14 at 09:38

1 Answers1

2

Your problem: tokens=* means "take the complete line", so delims=; has no effect.

Use this syntax instead:

for %%f in (%directory%) do echo %%f

The problem here is: you can not set any delimitors, it takes "standard" delimitors: space, comma, tab,;

So you have to enclose every token into doublequotes. Do do so, enclose the first string into doublequotes and replace every delimiter by ",". So c:\test1;c:\test2 becomes "c:\test1","c:\test2", what can be parsed easily. (the ~in %%~f removes the surrounding doublequotes)

for %%f in ("%directory:;=","%") do @echo %%~f
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    Just commenting that `tokens=*` actually means "take the complete line except remove any leading spaces" and `"delims="` means "take the complete line" which is not quite the context you mean, but it's true all the same. – foxidrive Aug 19 '14 at 13:04
  • @foxidrive I always considered `"tokens=*"` and `"delims="` to be interchangable. Another prove that "I know" is not the same as "It's a fact" ^^ Thanks for that info. – Stephan Aug 19 '14 at 15:50