1

I'm calling a batch file with two params, like this:

C:\myBatch.bat username hello this is text

Now i want to access username (no problem, %1). But i want now to get all other words in one second parameter. If I call %2, there is only "hello". And it is not possible that I call the batch file with double quotes around the text...

Any Idea? (something like a while %i != "" then do it in one var..)?

3
  • What's the problem with wrapping the parameters with double-quotes? Commented Feb 2, 2011 at 18:55
  • Its because it is a "routine job" for me and a friend of mine. This means, it has to be callable like net send was in XP (net send user text text text), there doesn't have to be quotes and it works too with the whole sentense. Commented Feb 2, 2011 at 19:05
  • You can do that. What you do is concatenate the parameters in your script, but separate them with a space. Just be aware that this isn't as powerful as having the user wrap the text with double quotes, because it means you must discard the user's whitespace and replace it with a single space. For example, two consecutive spaces become one space, a newline followed by a tab become one space etc. Commented Feb 4, 2011 at 20:11

3 Answers 3

4

Just using double quotes is the proper way to pack a space separated string into a single parameter, but as a alternative, you can use FOR to parse the "all parameters variable" (%*).

@echo off
FOR /F "tokens=1,*" %%A in ("%*") do (
    echo.Username=%%A
    echo.Message=%%B
)

When called as test.cmd nickname foo bar baz it prints:

Username=nickname 
Message=foo bar baz

I guess that is what you want?

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

Comments

2

Put the second parameter in double quotes when calling the batch script.

In the batch script use %~2 to reference the second parameter without the quotes.

If you want to use double quotes inside the quoted parameter, duplicate the quote character. With the dequoting in the batch script duplicated quotes are removed too, AFAIK.


Here's a simple script illustrating the idea:

@ECHO OFF
ECHO %1 %2
ECHO %~1 %~2

Now call it like this:

mybatch.bat username "hello this is text"

and see the result:

username "hello this is text"

username hello this is text

That is, when you use %i to reference a parameter, the quotes, if there are any, are retained, but %~i effectively removes them for you.

And, as you can see from %~1 use, if there are no quotes, the output is just the same as with %1, so you don't need to always include quotes simply to justify the tilde use.

Comments

1

Based on this doc http://ss64.com/nt/syntax-esc.html double quotes should work.

1 Comment

yes, but how I wrote, it has to work whitout double quotes, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.