what regular of conversion of special characters under enable delayed expansion is

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

what regular of conversion of special characters under enable delayed expansion is

#1 Post by sincos2007 » 11 May 2019 14:41

code:

Code: Select all

:test1
setlocal EnableDelayedExpansion

set "filename=temp1.txt"
set "str1=" & <"%filename%" set /p "str1="

echo output 1

set str1
echo !str1!
echo %str1%

echo output 2

set "str2=%str1%"
set str2
echo !str2!
echo %str2%

endlocal
goto :eof

content of temp1.txt

Code: Select all

   abc   %%var1%%   ^!^!var2^!^!   mmm   !var3!

output:

Code: Select all

output 1
str1=   abc   %%var1%%   ^!^!var2^!^!   mmm   !var3!
   abc   %%var1%%   ^!^!var2^!^!   mmm   !var3!
   abc   %%var1%%   var3
output 2
str2=   abc   %%var1%%   !!var2!!   mmm
   abc   %%var1%%   !!var2!!   mmm
   abc   %%var1%%      mmm

I want to know how does the code above work and why it print such output.

Is it some regular in it?

Thanks

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: what regular of conversion of special characters under enable delayed expansion is

#2 Post by pieh-ejdsch » 13 May 2019 10:35

Hallo sincos2007,
there are a few more options:

temp1.txt

Code: Select all

   abc   %%var1%%   !^!!var2 ^!^!^mmm !^mmm !var3 !^!^!varNotdefined !.! _neverVar !^^!-
specialC.cmd

Code: Select all

@echo off
pushD "%~f0\.."
:test1
setlocal
set "var1 =expanded001 "
set "var2 =expanded002 "
set "var2 ^=002andEcspe "
set "^=escapeOnly "
set "^^=escapeTwice "
set "^mmm =escapeHmmmmmm "
set "mmm =Hmmmmmmm "
set "var3 =expanded003 "
set ".=... "
set var
set ^^
set mm
set .
setlocal EnableDelayedExpansion

set "filename=temp1.txt"
<"%filename%" set /p "str1="

echo output 1
set str1
echo ----- original ----
echo  !str1!
echo ----- other 2 ----- 4x
echo   %str1%
echo  ^"%str1%
echo  ^^!%str1%
echo ^"^^!%str1%
echo ----- other 3 ----- 4x
echo   ^!%str1%
echo ^" !%str1%
echo ^" ^!%str1%
echo   ^!^"%str1%

echo ----- other 4 ----- 2x
echo " %str1%
echo "^!%str1%
echo ----- other 5 ----- 2x
echo "!%str1%
echo  ^!"%str1%
echo(

echo output 2
set "str2=%str1%"
set str2
echo !str2!
echo %str2%
echo(

echo output 3
set str3=%str1%
set str3
echo !str3!
echo %str3%

popD
pause
goto :eof
results

Code: Select all

var1 =expanded001
var2 =expanded002
var2 ^=002andEcspe
var3 =expanded003
^=escapeOnly
^mmm =escapeHmmmmmm
^^=escapeTwice
mmm =Hmmmmmmm
.=...
output 1
str1=   abc   %%var1%%   !^!!var2 ^!^!^mmm !^mmm !var3 !^!^!varNotdefined !.! _neverVar !^^!-
----- original ----
    abc   %%var1%%   !^!!var2 ^!^!^mmm !^mmm !var3 !^!^!varNotdefined !.! _neverVar !^^!-
----- other 2 ----- 4x
     abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
 "   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
 !   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
"!   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
----- other 3 ----- 4x
  expanded002 Hmmmmmmm mmm expanded003 .!-
" expanded002 Hmmmmmmm mmm expanded003 .!-
" expanded002 Hmmmmmmm mmm expanded003 .!-
  expanded002 Hmmmmmmm mmm expanded003 .!-
----- other 4 ----- 2x
"    abc   %%var1%%   escapeOnly 002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -
"!   abc   %%var1%%   escapeOnly 002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -
----- other 5 ----- 2x
"!002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -
 !002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -

output 2
str2=   abc   %%var1%%   escapeOnly 002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -
   abc   %%var1%%   escapeOnly 002andEcspe !mmm escapeHmmmmmm var3 escapeOnly !varNotdefined ...  _neverVar escapeTwice -
   abc   %%var1%%   escapeOnly 002andEcspe varNotdefined ...  _neverVar escapeTwice -

output 3
str3=   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .!-
   abc   %%var1%%   expanded002 Hmmmmmmm mmm expanded003 .-
Drücken Sie eine beliebige Taste . . .
the explanations are on https://stackoverflow.com/questions/409 ... se-scripts
Phil

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

Re: what regular of conversion of special characters under enable delayed expansion is

#3 Post by sincos2007 » 15 May 2019 22:10

In ‘other 2’ in your code, can I understand in this way:

1. remove all caret
2. collapse all consecutive opening exclamation mark into one
3. expand variable between two exclamation marks from left to right

Post Reply