Get raw arguments or command in batch file

Viewed 493

In a batch file, is there any way to get the raw arguments or command? For example, given the following batch file:

echo "%1"

And then running:

my-file.bat ^1.1

I would like it to output ^1.1, but ^ is a special character—escape character—so it is evaluated and %1 contains 1.1.

It is possible to fix this by escaping the caret when inputting the command (ex. my-file.bat ^^1.1), but this is not desired in my situation as I'm building a cross platform tool that also has a shell implementation (where providing ^1.1 works).

So I am wondering either:

  1. Is there a way to get the raw unevaluated argument text of "^1.1"?
  2. Or is there a way to get the raw command string or perhaps the last executed command text? I could then take this and do my own parsing.

Thanks!

1 Answers

Short: You can't

Long: You can't fetch any combination, but many.

The caret is the most problematic character, because it escapes the next character and is removed BEFORE the arguments are build.
Therefore it's not possible to get the raw, original input.

But there exist some more or less bullet proof techniques.
Best solution in most cases: SO:How to receive even the strangest command line parameters?
Has some minor drawbacks Same but can also fetch CR and LF

But if you call the batch file by an external process, you could be able to use the cmdcmdline variable, this is the best solution, as the arguments can be accessed really easy.

Btw. Later in your ode, you probably get another problem.
When you try to call another batch or executable file with exactly the same arguments, you have to escape them before.
Even that part is not easy

Related