need help - trying to call a macro from within a macro

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mirrormirror
Posts: 127
Joined: 08 Feb 2016 20:25

need help - trying to call a macro from within a macro

#1 Post by mirrormirror » 30 Jul 2016 11:58

So I'm just getting started with macros and there is no particular purpose in mind with these other than learning. I've taken jeb's :strlen function and converted it and also made a generic "macro1" to echo out some values. Both of these are working but I can't seem to call the %strlen% macro from within my generic macro. The script below tries a couple of variations. What am I doing wrong?

Code: Select all

@ECHO OFF &SETLocal DISABLEDelayedExpansion

SET LF=^


SET ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
SET "testString=this has a length of 23"

SET strLen=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
   (%\n%
      FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
         SET "s=!%%~b!#"%\n%
         SET "len=0"%\n%
         FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (%\n%
            if "!s:~%%P,1!" NEQ "" (%\n%
               SET /a "len+=%%P"%\n%
               SET "s=!s:~%%P!"%\n%
            )%\n%
         )%\n%
      )^&FOR %%v IN (!len!) DO ENDLocal ^&SET "%%a=%%v"%\n%
   )%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& SET argv=

SET "x="
SET "testString=this has a length of 23"
%strlen% x testString
@ECHO strlen result = %x%

SET macro1=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
   FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
      (@ECHO --- %%a=!%%a!   -   %%b=!%%b!)%\n%
   )^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=

SET str1=test1
SET str2=test2

%macro1% str1 str2

@ECHO(
SET macro2=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
   FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
      (@ECHO --- %%a=!%%a!   -   %%b=!%%b!)%\n%
      !!strlen!! x %%a %\n%
   )^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=

%macro2% str1 str2

@ECHO(
SET macro3=FOR %%# IN (1 2) DO IF %%#==2 (%\n%
   FOR /F "tokens=1,2" %%a IN ("!argv!") DO (%\n%
      (@ECHO --- %%a=!%%a!   -   %%b=!%%b!)%\n%
      %%strlen%% x %%a %\n%
   )^&ENDLocal%\n%
) ELSE SETLocal ENABLEDelayedExpansion ^& set argv=

%macro3% str1 str2
endlocal
OUTPUT:

Code: Select all

strlen result = 23
--- str1=test1   -   str2=test2

--- str1=test1   -   str2=test2
'FOR' is not recognized as an internal or external command,
operable program or batch file.

--- str1=test1   -   str2=test2
'%strlen%' is not recognized as an internal or external command,
operable program or batch file.

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

Re: need help - trying to call a macro from within a macro

#2 Post by jeb » 31 Jul 2016 03:23

It seems no possible to call a macro from within another macro.
As a macro has to be expanded in the percent phase, it can't expand another macro in a delayed way.

mirrormirror
Posts: 127
Joined: 08 Feb 2016 20:25

Re: need help - trying to call a macro from within a macro

#3 Post by mirrormirror » 31 Jul 2016 14:55

It seems no possible to call a macro from within another macro.

Ok, thank you - I won't waste any more time trying then.

If you ever manage to disprove this statement please let us know :)

Post Reply