Batch scripting - add special character to output for Codeception command - batch-file

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%"

Related

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.

Set variable to %%f [duplicate]

This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 4 years ago.
I set a variable in cmd with the set command, and tried to echo it.
Here is an example:
C:\Users\Logan>set var = text
C:\Users\Logan>set var
var = text
C:\Users\Logan>echo %var%
%var%
C:\Users\Logan>
Is there a way to force the cmd to echo the variable and not the raw text?
Assigning a value/string to an environment variable
It is best to use following syntax with command extensions enabled to define or modify an environment variable:
set "var=text"
The command is set and the parameter is "variable=value".
The parameter string can be enclosed in double quotes as on all commands as long as command extensions are enabled as by default.
If the double quotes are not used around variable=value, command set interprets everything to end of line after the first equal sign including not visible spaces and horizontal tabs at end of line as string value to assign to the variable.
The name of the variable starts with first non whitespace character (after double quote if used) and ends left to first equal sign. The value assigned to the variable starts right of first equal sign and ends at end of line or last double quote.
set VAR = TEXT
The command line above creates an environment variable with name VARSpace and assigns the string SpaceTEXT to this variable.
The usage of
set var="text"
is often not correct as this results in assigning to variable var the text with the quotes included and all trailing spaces and tabs. Referencing now var on another code line with surrounding quotes often results in an error message as the variable holds itself the text already with quotes. For more details see the answer on How to set environment variables with spaces?
Example:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "var=text"
set "var = text"
set "var1=and more text"
set var2="and more text"
set var3="text with 1 trailing space"
set var
echo var3=%var3%^<- Do you see the trailing space?
echo/
set UnwantedSpaceVar=Hello
echo %UnwantedSpaceVar%! ^<- Where does the space come from?
echo/
echo There is a trailing space after Hello in this code snippet.
echo/
set "TrailingSpacesIgnored=Hi"
echo %TrailingSpacesIgnored%! ^<- The 3 trailing spaces above are ignored.
echo/
endlocal
pause
Running this small batch code results in the output:
var=text
var = text
var1=and more text
var2="and more text"
var3="text with 1 trailing space"
var3="text with 1 trailing space" <- Do you see the trailing space?
Hello ! <- Where does the space come from?
There is a trailing space after Hello in this code snippet.
Hi! <- The 3 trailing spaces above are ignored.
Enclosing variable=value in quotes can be done even if the text itself contains 1 or more double quotes.
set "Quote=""
This line defines the variable Quote with the value ". Command set interprets everything after first equal sign left to last double quote as value to assign to variable with name between the first quote and first equal sign.
Note: A string value with " inside and next & or && or || can be even on usage of set "variable=value" misinterpreted and so result in unexpected behavior as it can be seen on running a batch file with following two lines:
#echo off
set "Variable=Value with one double quote in the middle" & echo Oh, there is something wrong here!"
Value with one double quote in the middle" & echo Oh, there is something wrong here! is the string to assign to the environment variable, but assigned to the variable is just Value with one double quote in the middle and the rest of the line after " in the middle and after & interpreted as conditional operator and not literally is interpreted as additional command to execute by cmd.exe. The same problem exists with && or || after a " with 0 or more spaces/tabs between. This problem is not caused by command set. It is caused by Windows command processor which splits the line up into a command line with set and one more command line with echo with conditional execution of the echo command line.
Variable assignment with disabled command extensions
The command syntax set "variable=value" cannot be used if the command extensions are disabled with setlocal DisableExtensions in the batch file (or in Windows registry which is very uncommon and never seen by me on any Windows computer). A syntax error would be the result on execution of the batch file.
It is only possible to use set variable=value with command extensions disabled whereby the value can contain also double quotes and care must be taken on trailing spaces/tabs as they are also assigned to the environment variable.
Run in a command prompt window cmd /? and setlocal /? for more information about command extensions and which commands are affected by command extensions. Note: The output list of affected commands misses exit as described in answers on Where does GOTO :EOF return to?
Variable assignment via arithmetic expression
Using command set with option /A changes completely the parsing of second argument, i.e. the string after set /A. With option /A as first argument the second string is interpreted as arithmetic expression and being therefore processed completely different than on assigning a string value to an environment variable. Environment variables are always of type string and never of type integer.
The usage of option /A requires enabled command extensions as otherwise the command set ignores the rest of the line completely without any error message.
It is in most cases not recommended to just assign a number to an environment variable using an arithmetic expression, i.e. using set /A var=1. set "var=1" or just set var=1 (and no trailing whitespaces) are a little bit faster because environment variables are always of type string.
In an arithmetic expression whitespaces are interpreted as delimiters for variable names, numbers and operators. For that reason the command line set /A var = 1 does not define a variable with name VARSpace with the string Space1 as set var = 1 does. set /A var = 1 defines a variable with name VAR with string value 1 after converting 1 from string (batch file contains 1 as character with hexadecimal code value 31) to integer with value 1 and back to a string with the two values 0x31 and 0x00 (string terminating null).
Variable assignment via a prompt
Also using command set with option /P changes the parsing of the
string after variable name and equal sign. The string after the variable name and the equal sign is interpreted as prompt text to output and not as string to assign to the environment variable.
The environment variable gets assigned the string entered by prompted user (or redirected from a file or command/application), or in case of user does not enter anything before pressing RETURN or ENTER, keeps its current value respectively is still undefined if not defined before the prompt.
The usage of option /P requires enabled command extensions as otherwise the command set ignores the rest of the line completely without any error message.
Most used syntax for prompting a user for a string is:
set /P var="Please enter something: "
The command set removes in this case automatically the double quotes around the prompt text before printing to handle STDOUT (console window if not redirected).
But working is also:
set /P "var=Please enter something: "
Please read this answer for more details about prompt text parsing and how to output a prompt text with surrounding double quotes.
You need to remove whitespace before and after =:
set "var=text"
echo %var%
text

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.
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.

Why is no string output with 'echo %var%' after using 'set var = text' command in cmd? [duplicate]

This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 4 years ago.
I set a variable in cmd with the set command, and tried to echo it.
Here is an example:
C:\Users\Logan>set var = text
C:\Users\Logan>set var
var = text
C:\Users\Logan>echo %var%
%var%
C:\Users\Logan>
Is there a way to force the cmd to echo the variable and not the raw text?
Assigning a value/string to an environment variable
It is best to use following syntax with command extensions enabled to define or modify an environment variable:
set "var=text"
The command is set and the parameter is "variable=value".
The parameter string can be enclosed in double quotes as on all commands as long as command extensions are enabled as by default.
If the double quotes are not used around variable=value, command set interprets everything to end of line after the first equal sign including not visible spaces and horizontal tabs at end of line as string value to assign to the variable.
The name of the variable starts with first non whitespace character (after double quote if used) and ends left to first equal sign. The value assigned to the variable starts right of first equal sign and ends at end of line or last double quote.
set VAR = TEXT
The command line above creates an environment variable with name VARSpace and assigns the string SpaceTEXT to this variable.
The usage of
set var="text"
is often not correct as this results in assigning to variable var the text with the quotes included and all trailing spaces and tabs. Referencing now var on another code line with surrounding quotes often results in an error message as the variable holds itself the text already with quotes. For more details see the answer on How to set environment variables with spaces?
Example:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "var=text"
set "var = text"
set "var1=and more text"
set var2="and more text"
set var3="text with 1 trailing space"
set var
echo var3=%var3%^<- Do you see the trailing space?
echo/
set UnwantedSpaceVar=Hello
echo %UnwantedSpaceVar%! ^<- Where does the space come from?
echo/
echo There is a trailing space after Hello in this code snippet.
echo/
set "TrailingSpacesIgnored=Hi"
echo %TrailingSpacesIgnored%! ^<- The 3 trailing spaces above are ignored.
echo/
endlocal
pause
Running this small batch code results in the output:
var=text
var = text
var1=and more text
var2="and more text"
var3="text with 1 trailing space"
var3="text with 1 trailing space" <- Do you see the trailing space?
Hello ! <- Where does the space come from?
There is a trailing space after Hello in this code snippet.
Hi! <- The 3 trailing spaces above are ignored.
Enclosing variable=value in quotes can be done even if the text itself contains 1 or more double quotes.
set "Quote=""
This line defines the variable Quote with the value ". Command set interprets everything after first equal sign left to last double quote as value to assign to variable with name between the first quote and first equal sign.
Note: A string value with " inside and next & or && or || can be even on usage of set "variable=value" misinterpreted and so result in unexpected behavior as it can be seen on running a batch file with following two lines:
#echo off
set "Variable=Value with one double quote in the middle" & echo Oh, there is something wrong here!"
Value with one double quote in the middle" & echo Oh, there is something wrong here! is the string to assign to the environment variable, but assigned to the variable is just Value with one double quote in the middle and the rest of the line after " in the middle and after & interpreted as conditional operator and not literally is interpreted as additional command to execute by cmd.exe. The same problem exists with && or || after a " with 0 or more spaces/tabs between. This problem is not caused by command set. It is caused by Windows command processor which splits the line up into a command line with set and one more command line with echo with conditional execution of the echo command line.
Variable assignment with disabled command extensions
The command syntax set "variable=value" cannot be used if the command extensions are disabled with setlocal DisableExtensions in the batch file (or in Windows registry which is very uncommon and never seen by me on any Windows computer). A syntax error would be the result on execution of the batch file.
It is only possible to use set variable=value with command extensions disabled whereby the value can contain also double quotes and care must be taken on trailing spaces/tabs as they are also assigned to the environment variable.
Run in a command prompt window cmd /? and setlocal /? for more information about command extensions and which commands are affected by command extensions. Note: The output list of affected commands misses exit as described in answers on Where does GOTO :EOF return to?
Variable assignment via arithmetic expression
Using command set with option /A changes completely the parsing of second argument, i.e. the string after set /A. With option /A as first argument the second string is interpreted as arithmetic expression and being therefore processed completely different than on assigning a string value to an environment variable. Environment variables are always of type string and never of type integer.
The usage of option /A requires enabled command extensions as otherwise the command set ignores the rest of the line completely without any error message.
It is in most cases not recommended to just assign a number to an environment variable using an arithmetic expression, i.e. using set /A var=1. set "var=1" or just set var=1 (and no trailing whitespaces) are a little bit faster because environment variables are always of type string.
In an arithmetic expression whitespaces are interpreted as delimiters for variable names, numbers and operators. For that reason the command line set /A var = 1 does not define a variable with name VARSpace with the string Space1 as set var = 1 does. set /A var = 1 defines a variable with name VAR with string value 1 after converting 1 from string (batch file contains 1 as character with hexadecimal code value 31) to integer with value 1 and back to a string with the two values 0x31 and 0x00 (string terminating null).
Variable assignment via a prompt
Also using command set with option /P changes the parsing of the
string after variable name and equal sign. The string after the variable name and the equal sign is interpreted as prompt text to output and not as string to assign to the environment variable.
The environment variable gets assigned the string entered by prompted user (or redirected from a file or command/application), or in case of user does not enter anything before pressing RETURN or ENTER, keeps its current value respectively is still undefined if not defined before the prompt.
The usage of option /P requires enabled command extensions as otherwise the command set ignores the rest of the line completely without any error message.
Most used syntax for prompting a user for a string is:
set /P var="Please enter something: "
The command set removes in this case automatically the double quotes around the prompt text before printing to handle STDOUT (console window if not redirected).
But working is also:
set /P "var=Please enter something: "
Please read this answer for more details about prompt text parsing and how to output a prompt text with surrounding double quotes.
You need to remove whitespace before and after =:
set "var=text"
echo %var%
text

How to use a directory that contains round brackets in a batch file

I have a batch file in which the 2 first lines read:
set DIRwhereRUN=C:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)
PUSHD %DIRwhereRUN%
but the batch does not work.
If I create a directory named testSTABLEunstable_WITHrandomBED and copy my stuff there everything works smoothly. Is there a way to make it work with the brackets? I don't want to rename for at least 2 reasons.
It's very difficult - and misleading - to isolate two lines as you have. There's nothing wrong with those two lines.
The difficulty that you are having is with "block" statements like
IF ... (something
something
somethinginvolving DIRWHERERUN
)
This is because batch substitutes the value of any %var% with its the-current value before executing the command(s) and hence misinterprets the ) in %Dirwhererun% as the closing-parenthesis of the IF (or ELSE or FOR.)
The way to overcome this is to "escape" %dirwhererun%'s ) (ie. temporarily suspend its special meaning) - this is done with the caret ^ - which itself is a special character (with the special meaning "the following character is just a character, not a special character".)
So - how to do this?
Here's a demonstration:
#ECHO OFF
setlocal
set "DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED^)"
SET dir
set DIRwhereRUN=U:\THISWILLBEWRONG(WITHrandomBED^)
SET dir
set DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED^^)
SET dir
set DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)
SET dir
MD %DIRwhereRUN%
PUSHD "%DIRwhereRUN%"
DIR
POPD
SET dirwhererun=%dirwhererun:)=^^)%
SET dir
(Note that I use U: as a temporary drive. I'm creating the directory using your original SET deliberately to show that it's not the SET or normal operations that are causing the problem)
Note that where the set uses quotes around the parameters, the value is applied literally. This form is often used to ensure that stray trailing spaces in lines are not included in the value set into the variable.
Note that the ^ seems ineffectual in the next set - because all it is doing is escaping the ) - and ) is NOT a special character in an ordinary SET
With the third version, the caret is included - but only one, because ^ escapes ^ and ) is an ordinary character.
Then we do all of the operations using the ) unadorned. Obviously attempting to re-create a directory is going to cause an error - but it's because the directory already exists, not because there's anything wrong with the command itself.
As demonstrated, the directory will be listed, so the PUSHD works correctly.
Finally, there's a method of setting a variable dynamically - possibly better set into another variable-name. This is useful where the variable may be read from a file or input from a user - that is, not specified literally.
Well - not quite finally. Two further quirks: First, % is not escaped by ^ but by %, and second, ECHO( appears to be the most flexible form of ECHO (where the character immediately following ECHO, normally space, but may be a number of others) - and doesn't participate in the statement-blocking mechanism.
( and ) are special characters to the shell, so you need to escape them. Cmd.exe's escape character is ^. So you can do the following:
set DIRWHERERUN=C:\UNIVERSITY\testSTABLEunstable^(WITHrandomBED^)
Bill
Use double quotes.
set "DIRwhereRUN=C:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)"
PUSHD "%DIRwhereRUN%"

Resources