Quote text with disappearing quotes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Quote text with disappearing quotes

#1 Post by jeb » 04 Dec 2011 07:56

Hi,

we know how to escape special characters like &|<>) with prefxing them with a caret or with enclose them into quotation marks.

Like in

Code: Select all

set "var=Test with &|<>"
or
echo "Test with &|<>

But in the echo example the output will also contain the quote.

Sometimes it would be nice to use quotes, but not to getting them into the result (I call them virtual quotes) :!:
There are two solutions, they are working nearly the same way.

Code: Select all

setlocal EnableDelayedExpansion
echo !"!  Test with &|<>
for /f %%^" in ("""") do echo  %%~Test with &|<>"

Both use that the parser detects in the special character phase the quotes in !"! or %%~", but later both are removed from the line.

The FOR/F style also works with disabledDelayedExpansion, but it's a bit too long for me,
but you can use it as a macro.

Code: Select all

set "print=for /f %%^" in ("""") do echo(%%~""
%print%Test with &|


This can also be useful, when you create larger macros.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set LF=^


::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"              The normal macro line end with multiline caret
set ^""\n=%%~"^^^%LF%%LF%^%LF%%LF%^^"    A macro line end with disappearing quote
set "$"=%%~""                                                         A macro line begin to virtual quote the line

set "CreateMacro=for /f %%^" in ("""") do "

%CreateMacro% set "$testMacro=( for /L %%n in (1 1 2) do echo Hello > out1.txt %"\n%
%$"% echo Also virtual quoted >> out1.txt %"\n%
echo Not quoted ^>^> out1.txt )
echo ------------------------
%$testMacro%
echo ------------------------


hope it helps
jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Quote text with disappearing quotes

#2 Post by Ed Dyreen » 04 Dec 2011 08:17


Thanks jeb :D

I had been looking for some "echo.inquotes" solution.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Quote text with disappearing quotes

#3 Post by dbenham » 04 Dec 2011 12:19

Inspired work jeb. :D

You have one small bug in your first FOR example - you are missing one quote. It should be

Code: Select all

for /f %%^" in ("""") do echo %%~"Test with &|<>"


I'm not sure if I prefer using the %$"% macro or simply using %%~" directly.

The technique can be manipulated to support both quoted and unquoted special characters. One other quote macro to consider is defining "" to be a normal quote plus a disappearing quote to perhaps simplify transition between quoted and unquoted special characters.

I suggest including " in the macro name so that the quote state machine is more obvious. Another option is to not include the disappearing quote in the macro definition at all and then require insertion of the disappearing quote only as needed.

Below are two sets of equivalent examples demonstrating all of the above. Both sets have both unquoted and quoted special characters. The only difference is whether the output starts quoted or not. Note how the closing disappearing quote is optional in the first set, as is often the case with the Windows quoting system.

Code: Select all

@echo off
setlocal
set ^"print"=for /f %%^" in ("""") do echo(%%~""
set "print=for /f %%^" in ("""") do echo("
set """="%%~""
set "$"=%%~""

%print"%Test with unquoted &|<> and quoted %%~""&|<>%%~"" and unquoted &|<> again%%~"
%print"%Test with unquoted &|<> and quoted %$"%"&|<>"%$"% and unquoted &|<> again%$"%
%print"%Test with unquoted &|<> and quoted %""%&|<>%""% and unquoted &|<> again%%~"

%print"%Test with unquoted &|<> and quoted %%~""&|<>%%~"" and unquoted &|<> again
%print"%Test with unquoted &|<> and quoted %$"%"&|<>"%$"% and unquoted &|<> again
%print"%Test with unquoted &|<> and quoted %""%&|<>%""% and unquoted &|<> again

%print%%%~"Test with unquoted &|<> and quoted %%~""&|<>%%~"" and unquoted &|<> again%%~"
%print%%$"%Test with unquoted &|<> and quoted %$"%"&|<>"%$"% and unquoted &|<> again%$"%
%print%%%~"Test with unquoted &|<> and quoted %""%&|<>%""% and unquoted &|<> again%%~"

%print%%%~"Test with unquoted &|<> and quoted %%~""&|<>%%~"" and unquoted &|<> again
%print%%$"%Test with unquoted &|<> and quoted %$"%"&|<>"%$"% and unquoted &|<> again
%print%%%~"Test with unquoted &|<> and quoted %""%&|<>%""% and unquoted &|<> again

echo ----------------------------------------------------

%print"%%%~""Test with quoted &|<>%%~"" and unquoted &|<> %%~""and quoted &|<> again"
%print"%%$"%"Test with quoted &|<>"%$"% and unquoted &|<> %$"%"and quoted &|<> again"
%print"%%""%Test with quoted &|<>%""% and unquoted &|<> %""%and quoted &|<> again"

%print%"Test with quoted &|<>%%~"" and unquoted &|<> %%~""and quoted &|<> again"
%print%"Test with quoted &|<>"%$"% and unquoted &|<> %$"%"and quoted &|<> again"
%print%"Test with quoted &|<>%""% and unquoted &|<> %""%and quoted &|<> again"

So many styles to choose from :!: :roll:
I'm not sure which I like best :?

One of the biggest benefits that I see for macro development is simplifying the definition of a macro that is defined using delayed expansion. The technique should make it possible to always escape ^ and ! only once, regardless if quoted or unquoted.Wrong :!: The disappearing FOR quotes are removed prior to the delayed expansion phase, so they won't do any good. :(

But the !"! solution works just fine :)

Code: Select all

@echo off
setlocal enableDelayedExpansion
echo !"!Test with unquoted ^^&|<>^! and quoted!"! "^^&|<>^!" !"!and unquoted ^^&|<> again^!!"!
echo "Test with quoted ^^&|<>^!" !"!and unquoted ^^&|<>^! !"!"and quoted ^^&|<> again^!"
I'm just not sure if it is any easier to read or write.

Dave Benham
Last edited by dbenham on 05 Dec 2011 07:45, edited 3 times in total.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Quote text with disappearing quotes

#4 Post by orange_batch » 04 Dec 2011 16:30

Jeb, in my larger scripts I use an echo function that I call upon which does this basically:

Code: Select all

@echo off&setlocal enabledelayedexpansion

call :echo "Hello World. &|<>"
pause
goto :eof

:echo
set "myecho=%~1"
echo:!myecho!
exit/b

See any problem with that?

jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Quote text with disappearing quotes

#5 Post by jeb » 05 Dec 2011 03:28

Hi orange_batch,

I see only one problem with carets, as a call double all carets, you are not able to echo a single caret with your function.

Code: Select all

call :echo "Hello World. &|<>^"

Output wrote:Hello World. &|<>^^

There could be also a minor problem using echo: instead of echo(, as echo: isn't safe against any content.

jeb

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Quote text with disappearing quotes

#6 Post by orange_batch » 05 Dec 2011 06:19

I see, and you're right about echo, I use : as a personal aesthetic choice but in a function I should use the better option.

Can you link me to the thread where you looked more in-depth about echo?

--Nevermind, found them. I'll leave it here for reference.
(older) viewtopic.php?t=774
viewtopic.php?t=1243

Post Reply