How do you escape a double-colon in a DOS batch file? - 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.

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.

Related

How to escape escape "=" or Equal to sign in batch script in a double quote string

SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
echo %gmail5%
The output is
H:\local\CODE\Batch scripting\powershell\Config>.\test.bat
H:\local\CODE\Batch scripting\powershell\Config>SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
H:\local\CODE\Batch scripting\powershell\Config>echo https://mail.google.com/mail/u/5/?tab=wm & ogbl#inbox
https://mail.google.com/mail/u/5/?tab=wm
'ogbl#inbox' is not recognized as an internal or external command,
operable program or batch file.
I checked the StackOverflow most of the post said that anything in "" is escaped along with =. I cannot figure out,why it gets recognized in echo. My use case is to use these strings in another batch script for vdesk.
vdesk create:4
vdesk on:1 run:%gmail5%
Batch uses & to separate different commands on one line. It does not assume that "..." is a string - it might be, however unlikely, that the " is a legitimate character as a command parameter.
Hence, you need to escape the & with a caret (^) which should work with all "awkward" characters except % for which the escape is % itself.
the thing you are trying to accomplish can be done in following way,your code was all right except every time when you print special characters use ^ or Cmd will confuse it for &(And) operater.
#echo off
SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm^&ogbl#inbox"
echo %gmail5%
pause >nul

Batch scripting - add special character to output for Codeception command

I am writing a bat file to automate the process of the below Codeception command.
php vendor/bin/codecept run tests/acceptance/SigninCest.php:^anonymousLogin$
The problem is that I cannot output the ^ character for example:
set functionNamePrefix=^^
set output=php vendor/bin/codecept run tests/acceptance/SigninCest.php:
set functionName=anonymousLogin
set functionNamePostFix=$
set command=%output%%functionNamePrefix%%functionName%%functionNamePostFix%
the $ symbol is correctly displayed but the ^ is not.
Any advice?
Thanks
Enclose the variable in quotes:
set "functionNamePrefix=^^"
Now the variable %functionNamePrefix% will contain ^.
Special characters such as the %|^ are seen as operators to cmd.
When you set functionNamePrefix=^^ and try to echo it, you effectively allow cmd to utilize the special character. Therefore, echo %functionNamePrefix% will give the more prompt, as cmd is expecting the next input line because of the ^.
When however you double quote a string, you are excluding the character in the current cmd run. It is however also recommended to double quote strings when using set to ensure you eliminate unwanted whitespace. For instance:
set var=value
Note the accidental space after value, this space will form part of the value as long as it exists, so enclose everything in double quotes to play safe and to ensure the special characters are not becoming functions in the current batch run.
set "functionNamePrefix=^^"
set "output=php vendor/bin/codecept run tests/acceptance/SigninCest.php:"
set "functionName=anonymousLogin"
set "functionNamePostFix=$"
set "command=%output%%functionNamePrefix%%functionName%%functionNamePostFix%"

Windows Batch - Protect special characters in a variable

A command needs to use a sequence looking like this one: Z;[%&.:mn=WO. The following call works:
D:\Some_folder> some_command -s "Z;[%&.:mn=WO"
The aim is to embed this call in a batch file, whose behavior isn't than the interactive mode one.
As you can see, there are several special characters, so it has to be protected before use. The batch file looks like the sample below:
set SEQUENCE=<sequence>
call %SOMEREP%\command -s %SEQUENCE% REM blah blah blah
Using set SEQUENCE="Z;[%&.:mn=WO" will fail, and surprisingly, with echo on, the displayed line before execution is exactly the same as the one in interactive mode, which works. Actually, the command line complains that the sequence should only contains ASCII characters in a range of 33-256, and be exactly 12 characters length. As is the quotes became a part of the value.
The attempt to protect each characters fails, with or without quotes:
With set SEQUENCE=Z^;[^^%%^&.:mn^=WO, the line will be:
D:\Some_folder> some_command -s Z;[% & .:mn=WO
It produces the error "The system cannot find the drive" (sounds like .: is interpreted as a drive switching)
With set SEQUENCE="Z^;[^^%%^&.:mn^=WO", the line will be:
D:\Some_folder> some_command -s "Z^;[^^%^&.:mn^=WO"
It complains about the sequence size, as well as the first attempt
Other character escaping attempt were tried without success, like quote escaping inside quoted string (""). What point is missed here? Asking before rewriting it in Bash.
Update: I noticed this morning that the reproduced attempts show a double caret (^^). I don't recall why it was done. I let it in place.
Within a batch-file, cmd will consume the first % so just double up on it:
set "SEQUENCE=Z;[%%&.:mn=WO"
some_command -s "%SEQUENCE%"
or if you want to retain the double quotes as part of the value, which is not preferred though:
set SEQUENCE="Z;[%%&.:mn=WO"
some_command -s %SEQUENCE%
If needed to use it without the surrounding double quotes, directly from the command, you need to double up on % and escape the & else the ampersand will be seen as a chaining operator:
some_command -s Z;[%%^&.:mn=WO
a Good source, in table form, for all of the escape sequences can be found on Robvanderwoude
EDIT
To explicitly call it, simply double on the variable % again:
set SEQUENCE="Z;[%%&.:mn=WO"
call some_command -s "%%SEQUENCE%%"
Calling the command is generally unnecessary unless you have variables set in a loop and delayed expansion isn't enabled (I used to explicitly avoid delayed expansion and use calls then I needed it instead.)
Assuming that %SOMEREP% was not set in a code block (and therefore no need to Call it) then the following should do.
Essentially just double the % once on the Variable, unless you really do need to call, in which case you quadruple it.
No Call:
Set "_CMD=%SOMEREP%\command"
Set "_SEQUENCE=Z;[%%&.:mn=WO"
"%_CMD%" -s "%_SEQUENCE%"
REM blah blah
Assuming you have actual need to call the command, the following escapes the % twice by using the %%%%
Set "_CMD=%SOMEREP%\command"
Set "_SEQUENCE=Z;[%%%%&.:mn=WO"
call "%_CMD%" -s "%_SEQUENCE%"
REM blah blah
Ignore the original bit about the carrots, I wrote this as I was waking this AM and apparently my brain was still thinking about some commands I was runnign the day prior and got confused, as at the CMD CLI you need to use ^ to escape % to force the delayed expansion of some variables.

echo in cmd file produces incorrect results

My .cmd file contains:
set /A "#zz+=1"
set #zz
echo:"%%#zz%%"
Produces:
#zz=4
"%#zz%"
The set #zz shows the variable being properly populated, but echo acts as though it is not there. Curiously, the commands work fine when run from the command prompt.
Actually, batch is doing exactly what it's supposed to. While the normal escape character in batch is ^, you escape % in batch scripts like %%, so you're telling the script to print a literal %, then the string #zz, then another literal %.
If you really want to echo %4%, you need three % signs on each side: echo:"%%%#zz%%%"
This tells batch to print a literal %, then the value of %#zz%, and then another literal %.

Replacing a symbol with pipe in a string batch

I have this string, call it str, which may look like this XinfoX. i want a command, using batch, which replaces the 'X's with this symbol '|' (a pipe symbol). i have tried this, but the cmd keeps closing/crashing.
thanks in advance
use variable edit/replace see here for details
set str=XinfoX
echo %str:X=^|%
C:\>echo %str:X=^|%
|info|
Your command is crashing because the | character functions as the pipe operator. But you want to use it as a literal character instead. The character must either be escaped or quoted.
set str=XinfoY
:: using quotes
set "str=%str:X=|%"
:: using escape
set str=%str:Y=^|%
The situation can get complicated if your string contains quotes such that some of the string is quoted and some is not. The solution is to use delayed expansion.
setlocal enableDelayedExpansion
set str="Xinfo1X"info2X
set "str=!str:X=|!"

Resources