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 %.
Related
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
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'm trying to pass through caret chars through batch.
Escaping them once would be easy, but I need to do it twice.
I have an executable that will back up tables based on a Regex expression (not my code).
I want to back up all tables with an exclusion list.
Using ^(?!tableName$).* works for a single table.
Batch File 1 (called from command line)
SET ignoreTables=tableName
:: Call the backup script
CALL SecondBatch.bat %ignoreTables%
Batch File 2
:: Passthrough ignoreTables
Executable.exe --ignoreTablesPattern="^(?!%1$).*"
But I'd like to ignore multiple tables. In Regex this means using the | (pipe) character eg; tableOne|tableTwo would require;
SET ignoreTables=tableOne^|tableTwo
Which is correct at the SET but not when passed to the CALL
The correct output that works from the command line is;
Executable.exe --ignoreTablesPattern="^(?!tableOne|tableTwo$).*"
How can I get this result out of the batch file?
In batch file 1 use:
SET "ignoreTables=tableOne|tableTwo"
:: Call the backup script
CALL SecondBatch.bat "%ignoreTables%"
And in batch file 2 use:
:: Passthrough ignoreTables
Executable.exe --ignoreTablesPattern="^(?!%~1).*$"
Run in a command prompt window cmd /? and read the output help pages, especially the last paragraph on last help page which is about when surrounding a directory/file name or parameter string with double quotes is required.
The first line of batch file 1 contains the command SET with the parameter string variable=value. By enclosing this parameter string in double quotes the pipe character is not interpreted anymore as operator. For more details about using double quotes on assigning a string to an environment variable see answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The value of the environment variable is passed next expanded as first parameter to batch file 2. Again surrounding double quotes are needed to pass the string containing | as literal string to the second batch file.
In the second batch file it is necessary to reference the first argument without surrounding quotes. Therefore %~1 is used now instead of %1 as explained in help of command CALL output on running in a command prompt window call /?.
BTW: I'm quite sure $ should be at end of the regular expression and not inside the negative lookahead.
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.