0

I want to run a script which only purpose is to execute its first argument, so I have the following script:

set command=%~1
%command%

And I run it like RunCommand.bat "echo hello world" which works.

Now I want to escape any special char, for example double quote. I tried a couple of options but non works. Any ideas?

This is my closest: RunCommand.bat "echo ""special char""" which prints-> ""special char""

EDIT

This script works

set "command=%~1"
set "command=%command:""="%"
%command%

refferenced from Escaping Double Quotes in Batch Script

omriman12
  • 1,644
  • 7
  • 25
  • 48
  • To start with `set command=%~1` should more correctly be `Set "FirstArgument=%~1"`. Using the doublequotes is the recommended syntax when defining a variable. As for your question you should really define 'any special char'. _Please do not try to make a batch file which accepts exactly the same commands as `cmd.exe`, but makes it even more complex, than just typing them into `cmd.exe`.!_ – Compo May 25 '20 at 15:33
  • @Compo we have a mechanizem which work with running batch files. We want to use this mechanizem to give our user the option to run any command, right now I am investigating it, If this is not achievable so its something I want to know Basically the requirements are running every command the same way a `cmd.exe` will run it – omriman12 May 26 '20 at 05:59
  • omrimann12, please clarify your question, I've already asked you to do that , but you haven't, you've made it worse. Your question title and code seems to be concentrating on escaping doublequotes in a predefined variable. Your comments and question body, seems to want something which can accept **any/every** command and pass it to cmd.exe without any potential poison characters affecting it. Please edit your title and code to suit the task, or rewrite the question body to suit the title and code. – Compo May 26 '20 at 10:00
  • I see this question every year, _as an assignment_, the end user is supposed to enter their command at the `Set /P` prompt, and then it is run. If the end user is typing it at a prompt, they could just do that directly in `cmd.exe` without any escaping or intervention prior to doing so. Your example seems to be using input already at the command prompt, or from another script. It is even more of a waste of time, why would I type `call allcommands "mycommand"` to run a batch file, (`allcommands`), which will then run `mycommand` for me? when I could just run `mycommand` or `cmd /k "mycommand"`! – Compo May 26 '20 at 10:06

1 Answers1

1

No need to escape anything.

runcommand.bat:

@%*

(yes, that's all)

Output:

C:\temp>runcommand.bat echo "hello World!"
"hello World!"

C:\temp>runcommand.bat ping -n 1 www.google.com

Ping wird ausgeführt für www.google.com [216.58.205.228] mit 32 Bytes Daten:
Antwort von 216.58.205.228: Bytes=32 Zeit=13ms TTL=57

Ping-Statistik für 216.58.205.228:
    Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 13ms, Maximum = 13ms, Mittelwert = 13ms

C:\temp>runcommand.bat wmic os get serialnumber /value


SerialNumber=00248-80000-00000-AR7GX

In response to my bat file must receive its variable inside a double quote: I see no benefit in complicating things with additional quotes, when not needed, but here you go (still no escaping needed):

@cmd /c %*

Output:

C:\temp>runcommand "echo "special char"&echo hello"
"special char"
hello
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • this is nice, but, one thing is that my bat file must receive its variable inside a double quote like so: `runcommand.bat "my arg"`. I did update a working script – omriman12 May 26 '20 at 15:37