Reputation: 1700
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
Upvotes: 0
Views: 524
Reputation: 56218
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
Upvotes: 1