Why it's important to always add an extra exclamation mark.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Why it's important to always add an extra exclamation mark.

#1 Post by Ed Dyreen » 25 Jul 2012 17:38

'
Spend like 3 hours searching some thread were jeb explained why it's important to always add an extra exclamation mark.
Had it documented but then deleted it because I thought I would remember such basic... :roll:
Didn't find the original thread I was after but then bumped into this post from jeb which comes pretty close and even has some WOW to it :) http://www.dostips.com/forum/viewtopic.php?p=6188#p6188
jeb wrote:

Code: Select all

SETLOCAL EnableDelayedExpansion
echo ^^^^--"^^" 1 "^^" ^^^^
echo ^^^^--"^^"   2 "^^" ^^^^ !
echo !^^^^--"^^"   3 "^^" ^^^^
echo:!^^^^--"^^"   4 "^^" ^^^^ WOW

--- OUTPUT ----
^^--"^^" 1 "^^" ^^
^--"^"   2 "^" ^
^--"^"   3 "^" ^
^--"^"   4 "^^" ^^ WOW

So I ended up writing my own little tutorial. I WOW somewhere else.

Code: Select all

@echo off
echo.
echo.An exclamation mark should always be added so that carets between quotes are
echo.always stripped in the same manner.
echo.
echo.disableDelayedExpansion doesn't care about the extra exclamation mark.
setlocal disableDelayedExpansion

set "$=with    exclamation mark ^ "^^^^"" !
set "$"
set "$=without exclamation mark ^ "^^^^""
set "$"
set "$=with    exclamation mark ^ "^^^^""
set "$=%$%" !
set "$"
set "$=without exclamation mark ^ "^^^^""
set "$=%$%"
set "$"

echo.
echo.But enableDelayedExpansion does !
setlocal enableDelayedExpansion

set "$=with    exclamation mark ^^ "^^^^^^^^"" !
set "$"
set "$=without exclamation mark ^^ "^^^^^^^^""
set "$"
set "$=with    exclamation mark ^^ "^^^^^^^^""
set "$=%$%" !
set "$"
set "$=without exclamation mark ^^ "^^^^^^^^""
set "$=%$%"
set "$"
echo.
echo.Some more interesting behavior of enableDelayedExpansion
echo.^^^^--"^^" 1 "^^" ^^^^
echo.^^^^--"^^" 2 "^^" ^^^^ !
echo.!^^^^--"^^"   3 "^^" ^^^^
echo !^^^^--"^^"   4 "^^" ^^^^ WOW
echo.
pause
exit

Code: Select all

An exclamation mark should always be added so that carets between quotes are
always stripped in the same manner.

disableDelayedExpansion doesn't care about the extra exclamation mark.
$=with    exclamation mark ^ "^^"
$=without exclamation mark ^ "^^"
$=with    exclamation mark ^ "^"
$=without exclamation mark ^ "^"

But enableDelayedExpansion does !
$=with    exclamation mark ^ "^^"
$=without exclamation mark ^^ "^^^^"
$=with    exclamation mark ^ "^"
$=without exclamation mark ^^ "^^"

Some more interesting behavior of enableDelayedExpansion
^^--"^^" 1 "^^" ^^
^^--"^^" 2 "^" ^
^--"^"   3 "^^" ^^
^--"^"   4 "^" ^ WOW

Druk op een toets om door te gaan. . .
These kind of things are key to understand when working with "special" characters that need to cross to the endlocal border.
http://www.dostips.com/forum/viewtopic.php?f=3&t=1910
Magic: all we don't understand.

There is interest in documenting various techniques but the problem is usually a lack of understanding and or time.
Recently I discover http://www.allenware.com/icsw/icswidx.htm ( which used to be my favorite DOS learning site as a beginner because of the way it looked and felt ) has now gone off-line before I had the chance to index and port some ideas. :(

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Why it's important to always add an extra exclamation ma

#2 Post by Liviu » 26 Jul 2012 19:07

Nice WOW factor.

Ed Dyreen wrote:Didn't find the original thread I was after

Perhaps this http://www.dostips.com/forum/viewtopic.php?p=7141#p7141 one?

Ed Dyreen wrote:Recently I discover http://www.allenware.com/icsw/icswidx.htm ( which used to be my favorite DOS learning site as a beginner because of the way it looked and felt ) has now gone off-line before I had the chance to index and port some ideas. :(

Last year snapshot at http://web.archive.org/web/20110719003330/http://www.allenware.com/icsw/icswidx.htm.

Liviu

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

Re: Why it's important to always add an extra exclamation ma

#3 Post by jeb » 27 Jul 2012 03:04

The link explained it nearly complete.

The cause for something like

Code: Select all

set "result=%var%" !


Is to be sure that independent of the content of var, the delayed phase will be reduce all carets a second time,
this is necessary, as all carets are doubled.

Code: Select all

setlocal DisableDelayedExpansion
set "var1=^x"
set "var2=^x!"
set "var1=%var1:^=^^%"
set "var1=%var1:!=^!%"
set "var2=%var2:^=^^%"
set "var2=%var2:!=^!%"
setlocal enableDelayedExpansion
set "result1a=%var1%"
set "result1b=%var1%" !
set "result2a=%var2%"
set result


As you can see it's necessary at result1b to add the exclamation mark, as you can't now if there is already an exclamation mark in the content.

jeb

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

Re: Why it's important to always add an extra exclamation ma

#4 Post by Ed Dyreen » 27 Jul 2012 08:54

'
I've added some code to clarify more cases where it's important to SHOUT :!:

Code: Select all

setlocal DisableDelayedExpansion
set "var1=^x"
set "var2=^x!"
set "var1=%var1:^=^^%"
set "var1=%var1:!=^!%"
set "var2=%var2:^=^^%"
set "var2=%var2:!=^!%"
setlocal enableDelayedExpansion
set "result1a=%var1%"
set "result1b=%var1%" !
set "result1c=!var1!"
set "result1d=!var1!" !
for %%? in ("%var1%") do set "result1e=%%~?"
for %%? in ("%var1%") do set "result1f=%%~?" !
for %%? in ("!var1!") do set "result1g=%%~?"
for %%? in ("!var1!") do set "result1h=%%~?" !
set "result2a=%var2%" !
set var
set result

Code: Select all

var1=^^x
var2=^^x^!
result1a=^^x
result1b=^x
result1c=^^x
result1d=^^x
result1e=^^x
result1f=^x
result1g=^^x
result1h=^x
result2a=^x!
I even discover a bug in my redelay function, can you imagine :shock:

Post Reply