0

I need to separate a string based on spaces, but only after the first space. However, the second string returns only one word.

The code I'm currently using is this:

@echo off set string=alone these are together for /f "tokens=1 " %%g IN ("%string%") do set first=%%g for /f "tokens=2*" %%g IN ("%string%") do set second=%%g echo %first% echo %second% pause 

Right now, my output is alone these, but I want alone these are together.

Am I setting the variable wrong, or is my sytax on the token option incorrect?

1 Answer 1

1

* is its own token in the token list, so "tokens=2*" %%g puts the second token in %string% in %%g and everything after it in %%h.

for /f "tokens=1,*" %%g IN ("%string%") do ( set "first=%%g" set "second=%%h" ) 

will put the first the first token in %first% and everything else in %second%.

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

1 Comment

Worked like a charm! Although my script needs a lot of these, so I found it easier to combine the 3 lines into one for /f "tokens=1,*" %%a in ("%string%") do (set "first=%%a" & set "second=%%b")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.