Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

preserving exclamation marks in variable between setlocals batch

Trying to preserve exclamation marks in variables. A simplified, illustrative script below:

    echo off
    set testvar="C:WindowsTestOfIllegals[!]"
    echo Pre-EnableDE: %testvar%

    setlocal enableextensions enabledelayedexpansion
    echo Post-EnableDE: %testvar%

    Setlocal DisableDelayedExpansion
    echo ssetlocal sub-instance...
    echo TestVar after re-disableDE: %testvar%
    set modTestVar=%testvar%
    echo TestVar to new var, modTestVar: %modTestVar%
    endlocal & set "RetVar2=%modTestVar%"

    echo modTestVar back in main script: %RetVar2%

    Setlocal DisableDelayedExpansion
    echo modTestVar, main script in another setlocal diasbleDE instance: %RetVar2%
    endlocal

    pause
    exit /b

this produces output:

    Pre-EnableDE: "C:WindowsTestOfIllegals[!]"
    Post-EnableDE: "C:WindowsTestOfIllegals[]"
    ssetlocal sub-instance...
    TestVar after re-disableDE: "C:WindowsTestOfIllegals[!]"
    TestVar to new var, modTestVar: "C:WindowsTestOfIllegals[!]"
    modTestVar back in main script: "C:WindowsTestOfIllegals[]"
    modTestVar, main script in another setlocal diasbleDE instance: "C:WindowsTestOfIllegals[]"

Why isn't the exclamation mark being preserved in modTestVar? Is there any way to do this?

(I know people have said before "post the full script" - but rather long and this represents the core issue. However, happy to post if helpful).

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's preserved in the variable, but you need to use delayed expansion.

When you used percent expansion in delayed expansion mode the variable will be expanded and the content, especially the exclamation marks will be parsed will be parsed also later, and a single exclamation mark will simply removed.

echo off
set testvar="C:WindowsTestOfIllegals[!]"
echo Pre-EnableDE: %testvar%

setlocal enableextensions enabledelayedexpansion
echo Post-EnableDE: !testvar!

Setlocal DisableDelayedExpansion
echo ssetlocal sub-instance...
echo TestVar after re-disableDE: %testvar%
set modTestVar=%testvar%
echo TestVar to new var, modTestVar: %modTestVar%
endlocal & set "RetVar2=%modTestVar%"

echo modTestVar back in main script: !RetVar2!

Setlocal DisableDelayedExpansion
echo modTestVar, main script in another setlocal diasbleDE instance: %RetVar2%
endlocal

pause

The other/only problem is when you try to transfer a variable over an endlocal barrier (like endlocal & set "RetVar2=%modTestVar%").
This is not trivial.

This is a batch macro for this, used like this %endlocal% modTestVar

setlocal DisableDelayedExpansion
set LF=^


set ^"
=^^^%LF%%LF%^%LF%%LF%^^"
%=   I use EDE for EnableDelayeExpansion and DDE for DisableDelayedExpansion =%
set ^"endlocal=for %%# in (1 2) do if %%#==2 (%
%
   setlocal EnableDelayedExpansion%
%
 %=       Take all variable names into the varName array       =%%
%
   set varName_count=0%
%
   for %%C in (!args!) do set "varName[!varName_count!]=%%~C" ^& set /a varName_count+=1%
%
 %= Build one variable with a list of set statements for each variable delimited by newlines =%%
%
 %= The lists looks like --> set result1=myContent
"set result1=myContent1"
set result2=content2
set result2=content2
     =%%
%
 %= Each result exists two times, the first for the case returning to DDE, the second for EDE =%%
%
 %= The correct line will be detected by the (missing) enclosing quotes  =%%
%
   set "retContent=1!LF!"%
%
   for /L %%n in (0 1 !varName_count!) do (%
%
      for /F "delims=" %%C in ("!varName[%%n]!") DO (%
%
         set "content=!%%C!"%
%
         set "retContent=!retContent!"set !varName[%%n]!=!content!"!LF!"%
%
         if defined content (%
%
 %=      This complex block is only for replacing '!' with '^!'      =%%
%
 %=    First replacing   '"'->'""q'   '^'->'^^' =%%
%
         set ^"content_EDE=!content:"=""q!"%
%
         set "content_EDE=!content_EDE:^=^^!"%
%
 %= Now it's poosible to use CALL SET and replace '!'->'""e!' =%%
%
         call set "content_EDE=%%content_EDE:^!=""e^!%%"%
%
         %= Now it's possible to replace '""e' to '^', this is effectivly '!' -> '^!'  =%%
%
         set "content_EDE=!content_EDE:""e=^!"%
%
         %= Now restore the quotes  =%%
%
         set ^"content_EDE=!content_EDE:""q="!"%
%
         ) ELSE set "content_EDE="%
%
         set "retContent=!retContent!set "!varName[%%n]!=!content_EDE!"!LF!"%
%
      )%
%
   )%
%
 %= Now return all variables from retContent over the barrier =%%
%
   for /F "delims=" %%V in ("!retContent!") DO (%
%
 %= Only the first line can contain a single 1 =%%
%
      if "%%V"=="1" (%
%
 %= We need to call endlocal twice, as there is one more setlocal in the macro itself =%%
%
         endlocal%
%
         endlocal%
%
      ) ELSE (%
%
 %= This is true in EDE             =%%
%
         if "!"=="" (%
%
            if %%V==%%~V (%
%
               %%V !%
%
            )%
%
         ) ELSE IF not %%V==%%~V (%
%
            %%~V%
%
         )%
%
      )%
%
   )%
%
 ) else set args="

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...