frank3579
frank3579

Reputation: 65

Reading second argument in batch file if first argument has commas

sample.bat a,b,c,d yes

I'm trying to output yes by echoing %2, but the result shows b. I think the comma is also counted as an escape character or something. How do I output yes as 2nd parameter?

Upvotes: 1

Views: 829

Answers (2)

jeb
jeb

Reputation: 82307

You could use %* instead and parse it with your own rules.

for /F "tokens=1,2 delims= " %%A in ("%*") DO (
  echo first=%%A
  echo second=%%B
)

Upvotes: 2

dbenham
dbenham

Reputation: 130829

The parameter delimiters are: , ; = <space> <tab> <0xFF>

The parameter delimiters cannot be modified, and they cannot be escaped.

The only way to include a delimiter within a parameter is to enclose the parameter in quotes.

sample.bat "a,b,c,d" yes

You can strip the quotes from the parameter by using the ~ modifier

echo %~1

Upvotes: 2

Related Questions