How to write to file text containing a double-quote without new line

Viewed 59

I need to write the following string without new line to the new file:

R"(

I have tried:

echo|set /p = R"( > ..\web\index.min.html
echo|set /p = R^"( > ..\web\index.min.html
echo|set /p = R\"( > ..\web\index.min.html

With no result.

But if I write:

echo|set /p = R""( > ..\web\index.min.html

I get:

R""(

Two double-quotes instead of one. How can I solve my problem?

2 Answers

In general it is better and more efficient to use <nul set /p =text instead of echo|set /p =text

The SET /P prompt has bizarre rules that are not consistent between Windows versions :(

It is best to put all redirection before the command so you that all chars after the = are part of the prompt, except for those cases referenced in the rules.

Things that are tricky and or cause problems are leading white space, leading =, and quotes.

Thankfully quotes can be solved rather easily. Either enclose your desired string to the right of the = in quotes:

<nul >..\web\index.min.html set /p ="R"("

The first quote will be stripped, and the last quote and all trailing characters after the last quote are stripped.

Or do the above and add an extra set of enclosing quotes around the entire construct, including the =:

<nul >..\web\index.min.html set /p "="R"(""

The first two quotes are stripped, and so are the last two quotes stripped, and all characters after the 2nd to last quote are ignored.

I don't recommend this, but each of the following would also work:

<nul >..\web\index.min.html set /p ="R"(" IGNORED
<nul >..\web\index.min.html set /p "="R"(" IGNORED " IGNORED

Your attempts fail because there is an (opening) quotation mark that hides the redirection expression > ..\web\index.min.html from the parser. Using "" works because there is a closing ", so the redirection is recognised.

Even normal escaping (like ^", not \") does not help here because there is a pipe (|) involved that executes either side in a new cmd instance, so escaping is already consumed by the parent instance and the (right) child instance receives an unescaped ".

Hence you need to do double-escaping to make it work:

echo/|set /P =R^^^"(> "..\web\index.min.html"

But if you avoid the pipe, single-escaping is enough:

< nul set /P =R^"(> "..\web\index.min.html"

Though moving the redirection expression to the front as shown in dbenham's answer is probably the best solution for this problem that I would also go for. Note that there are actually no additional quotation marks necessary:

< nul > "..\web\index.min.html" set /p =R"(
Related