Direct variable expansion inside variable definition ?

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:

Direct variable expansion inside variable definition ?

#1 Post by Ed Dyreen » 09 Jun 2011 13:19


This works:

Code: Select all

::--------------------------------------------------------------------------------------------------------------------------
%@Pre% @DelayedExpansion.Toggleoff
::(
echo on
   set ^"@DelayedExpansion.Toggleoff=( %$n1c%

      echo. ^&set /p "?= Delayed Expansion is" ^<nul %$n1c%

      if /i ["!"] neq [""] ( %$n1c%

         set "Expansion=disabled" %$n1c%
         set /p "?= [disabled]" ^<nul  %$n1c%

      ) else ( %$n1c%

         set "Expansion=enabled" %$n1c%
         set /p "?= [enabled]" ^<nul %$n1c%

         setlocal DisableDelayedExpansion %$n1c%

      ) %$n1c%

      echo. ^&set /p "?= Delayed Expansion is" ^<nul %$n1c%

      if /i ["!"] neq [""] ( %$n1c%

         set "Expansion=disabled" %$n1c%
         set /p "?= [disabled]" ^<nul  %$n1c%

      ) else ( %$n1c%

         set "Expansion=enabled" %$n1c%
         set /p "?= [enabled]" ^<nul %$n1c%

      ) %$n1c%
   )"
::)
%@Post% @DelayedExpansion.Toggleoff [OK]
::--------------------------------------------------------------------------------------------------------------------------
%@DelayedExpansion.Toggleoff%
pause
exit

This also works:

Code: Select all

::--------------------------------------------------------------------------------------------------------------------------
%@Pre% @DelayedExpansion.Get.State
::(
   set "@DelayedExpansion.Get.State=echo. &set /p "?= Delayed Expansion is" <nul &if /i ["!"] neq [""] ( set "Expansion=disabled" &set /p "?= [disabled]" <nul ) else ( set "Expansion=enabled" &set /p "?= [enabled]" <nul )"
::)
%@Post% @DelayedExpansion.Get.State [OK]
::--------------------------------------------------------------------------------------------------------------------------

::--------------------------------------------------------------------------------------------------------------------------
%@Pre% @DelayedExpansion.Toggleoff
::(
   set "@DelayedExpansion.Toggleoff=echo. &set /p "?= Delayed Expansion is" <nul &if /i ["!"] neq [""] ( set "Expansion=disabled" &set /p "?= [disabled]" <nul &setlocal DisableDelayedExpansion ) else ( set "Expansion=enabled" &set /p "?= [enabled]" <nul &setlocal DisableDelayedExpansion ) &%@DelayedExpansion.Get.State%"
::)
%@Post% @DelayedExpansion.Toggleoff [OK]
::--------------------------------------------------------------------------------------------------------------------------

I am simply trying to split it in two counterparts:

Code: Select all

::--------------------------------------------------------------------------------------------------------------------------
%@Pre% @DelayedExpansion.Get.State
::(
   set ^"@DelayedExpansion.Get.State=( %$n1c%

      echo. ^&set /p "?= Delayed Expansion is" ^<nul %$n1c%

      if /i ["!"] neq [""] ( %$n1c%

         set "Expansion=disabled" %$n1c%
         set /p "?= [disabled]" ^<nul  %$n1c%

      ) else ( %$n1c%

         set "Expansion=enabled" %$n1c%
         set /p "?= [enabled]" ^<nul %$n1c%

      ) %$n1c%
   )"
::)
%@Post% @DelayedExpansion.Get.State [OK]
::--------------------------------------------------------------------------------------------------------------------------

Code: Select all

::--------------------------------------------------------------------------------------------------------------------------
%@Pre% @DelayedExpansion.Toggleoff
::(
echo on
   set ^"@DelayedExpansion.Toggleoff=( %$n1c%

      echo. ^&set /p "?= Delayed Expansion is" ^<nul %$n1c%

      if /i ["!"] neq [""] ( %$n1c%

         set "Expansion=disabled" %$n1c%
         set /p "?= [disabled]" ^<nul  %$n1c%

      ) else ( %$n1c%

         set "Expansion=enabled" %$n1c%
         set /p "?= [enabled]" ^<nul %$n1c%

         setlocal DisableDelayedExpansion %$n1c%

      ) %$n1c%

      echo. ^&set /p "?= Delayed Expansion is" ^<nul %$n1c%

      %@DelayedExpansion.Get.State% %$n1c%
   )"

%@DelayedExpansion.Toggleoff%
pause
exit
::)
%@Post% @DelayedExpansion.Toggleoff [OK]
::--------------------------------------------------------------------------------------------------------------------------

Just doesn't work :(

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

Re: Direct variable expansion inside variable definition ?

#2 Post by Ed Dyreen » 10 Jun 2011 03:53

Is this the problem Benham was experiecing in

Batch "macros" with arguments
http://www.dostips.com/forum/viewtopic.php?f=3&t=1827
:?:
Is this why he needed a dereferencer ? Because this would work:
for ( *** ) !@othermacro!
But I can't use delayed here, obviously !

This should make clear what I want to do in disabled delayed:

Code: Select all

   set ^"@test1=( %$n1c%

      echo.hello1 %$n1c%
      echo.there1 %$n1c%
   )"

   set ^"@test=( %$n1c%

      echo.hello %$n1c%
      %@test1% %$n1c%
      echo.there %$n1c%
   )"

%@test%

echo.endoftest
pause
exit

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

Re: Direct variable expansion inside variable definition ?

#3 Post by Ed Dyreen » 10 Jun 2011 04:15

@Benham
So we agree that the macro must be fully expanded during definition. But what is the best way to do that.

I see how Ed is able to accomplish this with simple calls using delayed expansion. But I very much want to avoid delayed expansion during macro definition because:

1) I want the macro definitions to persist after the batch that defines them terminates.
2) Delayed expansion complicates the syntax of the definition for the reasons that Jeb has pointed out.

:idea: I am working on a 2 part macro definition mechansim. First the macro is defined with embedded macros referenced in some way that will not be expanded but that can be identified as a macro reference. Something like ${macroName}. Next I call a derefMacro function that recursively expands the ${macroName} references. The derefMacro function will need delayed expansion, but all of the complicated logic will be encapsulated in one place where it can be reused for many macro definitions. I'll post the results of my experiments later.

I think I'm on a dead end. sorry for bumping the topic :(

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

Re: Direct variable expansion inside variable definition ?

#4 Post by Ed Dyreen » 10 Jun 2011 05:56


I think I figured it out after all ( how to expand a variable in a varable using % ).

Code: Select all

@echo off &setlocal EnableDelayedExpansion

set ^"@DelayedExpansion.Get.State=for %%r in ( "^!" ) do if /i ["%%~r"] == [""] ( echo.enabled ) else echo.disabled "

set ^"@DelayedExpansion.Toggleoff=( %$n1c%

   echo.This %$n1c%
   %@DelayedExpansion.Get.State% %$n1c%
   setlocal DisableDelayedExpansion %$n1c%
   echo.Works %$n1c%
)"

%@DelayedExpansion.Toggleoff%
%@DelayedExpansion.Get.State%

pause
exit /b

But I am puzzled as to why I can't use linefeeds in the first variable definition :?:

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

Re: Direct variable expansion inside variable definition ?

#5 Post by dbenham » 10 Jun 2011 07:00

EdDyreen wrote:But I am puzzled as to why I can't use linefeeds in the first variable definition :?:

Because the lineFeeds need that funky looking escape sequence that jeb showed you if you want to access them without delayed expansion.

You will also get into trouble including previously defined macros if they contain any unquoted special characters unless you use delayed expansion at definition time. But then it becomes very difficult to make the macro definitions resident in your cmd session. (They disappear after ENDLOCAL). That is why I used my drefMacro.

Dave Benham

Post Reply