How to escape % inside double quotes in cmd? - windows

In cmd I'm trying to do something like
program.exe -command "otherprogram.exe %thing% %path%"
The issue I'm having is that I can't figure out how to escape the % characters when they're inside double quotes, but I need the double quotes because of the spaces in this argument. Basically I don't want cmd to do variable expansion before passing the argument value to program.exe.
Just to be clear, this is directly in cmd, not in a batch script.

A simple semi solution is:
program.exe -command ^"otherprogram.exe %th^ing% %pa^t^h%^"
The positions of the carets inside the variable name are random.
This still could fail, but only for the rare case, if variables exists named thi^ng or pa^t^h

It seems strange, but don't escape the percent signs. Put a caret (the escape sign for every other special char) anywhere within the variable name: echo %^username% or `echo %use^rname%.
The first parsing removes the ^ (because there is (hopefully) no variable with that name). On command line (other than in a batch file), an empty variable doesn't show nothing, but the variable name including the surrounding %'s.
Any further level of parsing then receives the "normal" variable and expands it. Prove:
echo %usern^ame%
call echo %usern^ame%
Not your question, but for the sake of completeness: in a batch script, simply escape the % with another %:
echo %%username%%
call echo %%username%%

Related

Interactive Command Prompt character escaping %

I need to escape the % character in interactive Command Prompt (NOT in a batch file). How do I echo the text a %pathext% b; %pathext% expands to the environment variable; ^, %, \ and "quotes" all do not work. Please base the answer on: echo a %pathext% b.
Many thanks in advance.
There is one simple solution, but it's not 100% bullet proof
echo a %path^ext% b
The position of the caret can be moved to any position.
This works, because the variable expansion on the command line works different than in batch files.
If an undefined variable should be expanded, in a batch file it results in an empty text, but on the cli the percent expression will be used unchaged.
The caret will be removed in a later step of the parser.
But it can still fail when there exists a variable named path^ext
For a bullet proof solution, you need to create a percent sign and expand it.
for /F "delims==" %# in ("%=%=") do echo a %#pathext%# b
echo a %^pathext% b
Use a caret inside the variable (it can be anywhere inside the % signs) to prevent the parser from recognizing and substituting the variable.

Use an environment variable in a windows cmd file as substring length parameter

I need to output the first X characters of the content of OLD_ENTRY, say 33 chars. I grab the number of chars with another script. What is, in the following command in a Windows cmd script, the correct syntax to use a variable, say POS, instead of the hardcoded value 33?
echo %OLD_ENTRY:~0,33%
Thanks for any help,
Rip
Alright, I tend to call something like this as "nested variables". Anyway, to expand such nested variables, you need to establish a second parsing or expansion phase, and you need to ensure that the inner variable (POS) becomes expanded first, and the outer one (OLD_ENTRY) becomes expanded during the second phase. There are some options:
Using call:
This option avoids delayed variable expansion, which could be problematic with literal ! symbols, but it is quite slow, and it doubles quoted ^ characters:
In command prompt window:
call echo %^OLD_ENTRY:~0,%POS%%
This looks like "escaping" (^) the outer variable, but actually, this has got nothing to do with true escaping. In command prompt, an undefined variable does not become replaced by an empty string, it is just kept literally. So in the first pass, the undefined variable ^OLD_ENTRY is simply kept (you can verify that by defining such a variable by set "^OLD_ENTRY=something"), scanning for the closing % is skipped after the : for undefined variables strangely, the variable %POS% becomes expanded, and the last (orphaned) % is kept too; immediately after this phase, the escape sequence ^O is recognised, which results in a literal O; so we get echo %OLD_ENTRY:~0,33%, which becomes expanded in the second pass.
In a batch file:
call echo %%OLD_ENTRY:~0,%POS%%%
In the first pass, the first two consecutive percent symbols become replaced by one literal % sign, the variable %POS% becomes expanded, and the remaining two consecutive percent symbols become replaced by one literal % sign too, so we have echo %OLD_ENTRY:~0,33%, which becomes expanded in the second pass.
Using delayed variable expansion:
This is the better option, I think, because it is faster and does not mess around with ^:
echo !OLD_ENTRY:~0,%POS%!
This option works in both command prompt window and batch files. Here the first pass is the normal/immediate expanssion (%) which handles the %POS% variable, so the second pass the delayed expansion (!) receives echo !OLD_ENTRY:~0,33! to expand.
Refer also to this post: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Call .exe from batch file with variable path containing spaces

I want to launch a windows executable from a batch file where the path to the executable is stored in a variable.
#echo off
set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath=%qtpath%\windeployqt.exe
echo %execpath%
%execpath% --someparams
Unfortunately executing my script throws an error:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Looks like somehow the string gets terminated at the space in Program Files.
You should change your code to this:
#echo off
set "qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin"
set "execpath=%qtpath%\windeployqt.exe"
echo "%execpath%"
"%execpath%" --someparams
The SPACE, like also TAB, ,, ;, =, VTAB (vertical tabulator, ASCII 0x0B), FF (form-feed, ASCII 0x0C) and NBSP (non-break space, ASCII 0xFF) constitute token separators in the command prompt cmd. To escape tokenisation enclose your path in between "". This avoids also trouble with special characters like ^, ( and ), &, <, > and |.
The quotation marks in the set command lines again avoid trouble with special characters; they do not become part of the variable value because they enclose the entire assignment expression. Note that this syntax requires the command extensions to be enabled, but this is the default anyway.
I recommend not to include the quotation marks into variable values (set VAR="some value"), because then you could run into problems particularly when concatenating strings due to unwanted (double-)quotation (for instance, echo "C:\%VAR%\file.txt" returns "C:\"some value"\file.txt").
You are perfectly right. If the path to the file you want to execute contains spaces, you have to surround it with quotation marks:
#echo off
set qtpath=C:\Program Files\Qt\5.7\mingw53_32\bin
set execpath="%qtpath%\windeployqt.exe"
echo %execpath%
%execpath% --someparams
This should work.
It will also work when you surround %execpath% with quotation marks:
"%execpath%" --someparams

Batch path variable with space - how to escape the double quotes?

I'm setting a variable with this directory
C:\My dir\Sub dir\Folder with a lot of spaces\file.html
like this:
SET location="C:\My dir\Sub dir\Folder with a lot of spaces\file.html"
The problem is, when I echo %location% it comes with both double quotes. I've tried to remove using this:
set location=%location:"=\"%
But it does not work. I've searched a lot and I can't find an easy and simple solution. I'm using Windows 8.
An easy and simple solution is to store the path without the quotes, like this:
SET "location=C:\My dir\Sub dir\Folder with a lot of spaces\file.html"
This way the value will be stored with no quotes and the SET statement will still be protected from the effect of certain special characters. You will then either enclose %location% in quotes or omit the quotes as required. For instance, if you are using the value as an argument of some command, you will likely need to put the quotes around it:
somecommand "%location%" …
…
And if you just need to output the path, the quotes may be unnecessary – and it's easy to leave them out:
ECHO Location: %location%
…
Note that if the path has characters with special meaning, like &, ECHOing the path without the quotes would cause such characters to be interpreted in % evaluation. To avoid that, you could temporarily enable and use delayed expansion:
SETLOCAL EnableDelayedExpansion
ECHO Location: !location!
ENDLOCAL
…
Use set "location=C:\My dir\Sub dir\Folder with a lot of spaces\file.html" to keep the spaces but leave out the double quotes.
Don't use quotes in Set.
Later when you use that variable you include quotes around it eg
type "%location%"
Also this removes quotes
set location=%location:"=%
Yours I think replaces quotes with a backslash and a quote, not very useful.
Why even put it into a variable?

Escape double quotes in parameter

In Unix I could run myscript '"test"' and I would get "test".
In Windows cmd I get 'test'.
How can I pass double-quotes as a parameter? I would like to know how to do this manually from a cmd window so I don't have to write a program to test my program.
Another way to escape quotes (though probably not preferable), which I've found used in certain places is to use multiple double-quotes. For the purpose of making other people's code legible, I'll explain.
Here's a set of basic rules:
When not wrapped in double-quoted groups, spaces separate parameters:program param1 param2 param 3 will pass four parameters to program.exe: param1, param2, param, and 3.
A double-quoted group ignores spaces as value separators when passing parameters to programs:program one two "three and more" will pass three parameters to program.exe: one, two, and three and more.
Now to explain some of the confusion:
Double-quoted groups that appear directly adjacent to text not wrapped with double-quotes join into one parameter:hello"to the entire"world acts as one parameter: helloto the entireworld.
Note: The previous rule does NOT imply that two double-quoted groups can appear directly adjacent to one another.
Any double-quote directly following a closing quote is treated as (or as part of) plain unwrapped text that is adjacent to the double-quoted group, but only one double-quote:"Tim says, ""Hi!""" will act as one parameter: Tim says, "Hi!"
Thus there are three different types of double-quotes: quotes that open, quotes that close, and quotes that act as plain-text.
Here's the breakdown of that last confusing line:
" open double-quote group
T inside ""s
i inside ""s
m inside ""s
inside ""s - space doesn't separate
s inside ""s
a inside ""s
y inside ""s
s inside ""s
, inside ""s
inside ""s - space doesn't separate
" close double-quoted group
" quote directly follows closer - acts as plain unwrapped text: "
H outside ""s - gets joined to previous adjacent group
i outside ""s - ...
! outside ""s - ...
" open double-quote group
" close double-quote group
" quote directly follows closer - acts as plain unwrapped text: "
Thus, the text effectively joins four groups of characters (one with nothing, however):
Tim says, is the first, wrapped to escape the spaces
"Hi! is the second, not wrapped (there are no spaces)
is the third, a double-quote group wrapping nothing
" is the fourth, the unwrapped close quote.
As you can see, the double-quote group wrapping nothing is still necessary since, without it, the following double-quote would open up a double-quoted group instead of acting as plain-text.
From this, it should be recognizable that therefore, inside and outside quotes, three double-quotes act as a plain-text unescaped double-quote:
"Tim said to him, """What's been happening lately?""""
will print Tim said to him, "What's been happening lately?" as expected. Therefore, three quotes can always be reliably used as an escape.However, in understanding it, you may note that the four quotes at the end can be reduced to a mere two since it technically is adding another unnecessary empty double-quoted group.
Here are a few examples to close it off:
program a b REM sends (a) and (b)
program """a""" REM sends ("a")
program """a b""" REM sends ("a) and (b")
program """"Hello,""" Mike said." REM sends ("Hello," Mike said.)
program ""a""b""c""d"" REM sends (abcd) since the "" groups wrap nothing
program "hello to """quotes"" REM sends (hello to "quotes")
program """"hello world"" REM sends ("hello world")
program """hello" world"" REM sends ("hello world")
program """hello "world"" REM sends ("hello) and (world")
program "hello ""world""" REM sends (hello "world")
program "hello """world"" REM sends (hello "world")
Final note: I did not read any of this from any tutorial - I came up with all of it by experimenting. Therefore, my explanation may not be true internally. Nonetheless all the examples above evaluate as given, thus validating (but not proving) my theory.
I tested this on Windows 7, 64bit using only *.exe calls with parameter passing (not *.bat, but I would suppose it works the same).
I cannot quickly reproduce the symptoms: if I try myscript '"test"' with a batch file myscript.bat containing just #echo.%1 or even #echo.%~1, I get all quotes: '"test"'
Perhaps you can try the escape character ^ like this: myscript '^"test^"'?
Try this:
myscript """test"""
"" escape to a single " in the parameter.
The 2nd document quoted by Peter Mortensen in his comment on the answer of Codesmith made things much clearer for me. That document was written by windowsinspired.com. The link repeated: A Better Way To Understand Quoting and Escaping of Windows Command Line Arguments.
Some further trial and error leads to the following guideline:
Escape every double quote " with a caret ^. If you want other characters with special meaning to the Windows command shell (e.g., <, >, |, &) to be interpreted as regular characters instead, then escape them with a caret, too.
If you want your program foo to receive the command line text "a\"b c" > d and redirect its output to file out.txt, then start your program as follows from the Windows command shell:
foo ^"a\^"b c^" ^> d > out.txt
If foo interprets \" as a literal double quote and expects unescaped double quotes to delimit arguments that include whitespace, then foo interprets the command as specifying one argument a"b c, one argument >, and one argument d.
If instead foo interprets a doubled double quote "" as a literal double quote, then start your program as
foo ^"a^"^"b c^" ^> d > out.txt
The key insight from the quoted document is that, to the Windows command shell, an unescaped double quote triggers switching between two possible states.
Some further trial and error implies that in the initial state, redirection (to a file or pipe) is recognized and a caret ^ escapes a double quote and the caret is removed from the input. In the other state, redirection is not recognized and a caret does not escape a double quote and isn't removed. Let's refer to these states as 'outside' and 'inside', respectively.
If you want to redirect the output of your command, then the command shell must be in the outside state when it reaches the redirection, so there must be an even number of unescaped (by caret) double quotes preceding the redirection. foo "a\"b " > out.txt won't work -- the command shell passes the entire "a\"b " > out.txt to foo as its combined command line arguments, instead of passing only "a\"b " and redirecting the output to out.txt.
foo "a\^"b " > out.txt won't work, either, because the caret ^ is encountered in the inside state where it is an ordinary character and not an escape character, so "a\^"b " > out.txt gets passed to foo.
The only way that (hopefully) always works is to keep the command shell always in the outside state, because then redirection works.
If you don't need redirection (or other characters with special meaning to the command shell), then you can do without the carets. If foo interprets \" as a literal double quote, then you can call it as
foo "a\"b c"
Then foo receives "a\"b c" as its combined arguments text and can interpret it as a single argument equal to a"b c.
Now -- finally -- to the original question. myscript '"test"' called from the Windows command shell passes '"test"' to myscript. Apparently myscript interprets the single and double quotes as argument delimiters and removes them. You need to figure out what myscript accepts as a literal double quote and then specify that in your command, using ^ to escape any characters that have special meaning to the Windows command shell. Given that myscript is also available on Unix, perhaps \" does the trick. Try
myscript \^"test\^"
or, if you don't need redirection,
myscript \"test\"
I'm calling powershell from cmd, and passing quotes and neither escapes here worked. The grave accent worked to escape double quotes on this Win 10 surface pro.
>powershell.exe "echo la`"" >> test
>type test
la"
Below are outputs I got for other characters to escape a double quote:
la\
la^
la
la~
Using another quote to escape a quote resulted in no quotes.
As you can see, the characters themselves got typed, but didn't escape the double quotes.
Maybe you came here, because you wonder how to escape quotes that you need in the command that you pass to /c on cmd.exe? Well you don't:
CMD /c "MKDIR "foo bar""
will execute
MKDIR "foo bar"
which is really a behavior that I did not expect in the first glance.

Resources