Search code examples
batch-fileescapingdos

How do you escape a double-colon in a DOS batch file?


I need to run a command in a DOS batch file that contains a double colon AND set the output to a variable. Like this

set /a TDR = C:\InCharge\CONSOLE\smarts\bin\dmctl -s SSA-SAM invoke SM_System::SM-System nameToAddr %SM_OBJ_InstanceName%

I keep getting "Missing operator". I assume that is due to those double-colons. How do I escape these? I tried back-slashes but that didn't work. I've tried putting the whole command in double-quotes and that also didn't work.

I can run the command by itself, ie without the "set /a TDR" and the output is correct. But I need to use that output as the value of a variable hence the "set /a"

Normal output for dmctl is this

{ "10.28.112.74" }

I am using dmctl to get the ip address for the hostname. I figured once I got the output I could strip off the brackets and quotations, but I haven't figured out how to grab the output.

Thank you in advance.


Solution

  • Colons do not need to be escaped. See Batch files - Escape Characters for details on what characters need to be escaped, and how to escape them.

    "Missing operator" is being returned because SET /A only works with arithmetic operations, so it is looking for an arithmetic operator.

    To assign the output of a command to a variable, you have to use the FOR command, similar to the following:

    for /f  "delims=" %%i in ('C:\InCharge\CONSOLE\smarts\bin\dmctl -s SSA-SAM invoke SM_System::SM-System nameToAddr %SM_OBJ_InstanceName%') do set myresult=%%i
    

    See Reading the output of a command into a batch file variable

    To trim 3 characters from the beginning and end of a string:

    set mystring=%mystring:~3,-3%
    

    This will remove the curly braces, spaces, and quotation marks that delimit the IP address in the output.

    I found this at DOS - String Manipulation.