I am trying to get escape character like below:
C:\\Program Files (x86)\\Java\\jdk1.7.0_25
Here is the code in batch script:
set AGNT_JAVA_HOME=%JAVA_HOME% SET
set AGNT_JAVA_HOME=%AGNT_JAVA_HOME:\\=\\\\%
But the value coming is :
AGNT_JAVA_HOME value is C:\Program Files (x86)\Java\jdk1.7.0_25
Any idea what need to be added here to get the value as first line.
The escape character for batch is ^, not \.
The \ literal does not require escaping.
So all you need is:
set AGNT_JAVA_HOME=%AGNT_JAVA_HOME:\=\\%
But it is safer to enclose the entire SET assignment in quotes, just in case AGNT_JAVA_HOME contains a poison character like &.
set "AGNT_JAVA_HOME=%AGNT_JAVA_HOME:\=\\%"
Related
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%"
I am trying to make a batch script, that creates a new batch script called MigrateOldStickyNotes.bat with a couple of simple commands in it.
#echo SET CP=%~dp0> "%cp%\%ME%\%ME%\MigrateOldStickyNotes.bat"
#echo SET ME=%Username%>> "%cp%\%ME%\%ME%\MigrateOldStickyNotes.bat"
But insteadt of creating af new batchfile with these to lines that looks exactly like this.
SET CP=%~dp0
SET ME=%Username%
It will create a new file and use the variable as input. which will result i these to lines.
SET CP=\\sosy-nas\Backup\
SET ME=itcebrha
How do i make the script treat the variable as string insteadt of treating it as a variale.
Escape the percent signs with a percent sign, like SET CP=%%~dp0.
All other (special) characters are escaped with a caret ^.
The cause is that percent signs are handled in another phase of the batch parser than all other charaters.
That's also the cause why escaping of percent signs works only in batch files, but not on the command line, as there the cmd-line parser have different expansion rules for percent signs.
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.
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 %.
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=|!"