Advertisement
T3RRYT3RR0R

MacroTemplate

Feb 9th, 2024 (edited)
1,090
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.92 KB | Source Code | 0 0
  1. @Echo off & Setlocal EnableExtensions & CLS
  2.  
  3. REM Author: T3RR0R, aka T3RRY. 10/02/2024
  4. REM Discuss with author at https://discord.gg/HCSEC829F9
  5. REM
  6. REM - A template for the creation of macros with arguments
  7. REM   Macros may be defined in either Delayed enabled or disabled environments, but will require
  8. REM   Delayed expansion to be enabled in order for them to be expanded.
  9. REM  
  10. REM   EXPLAINER
  11. REM   - Macro's offer superior performance to CALLed :functions, however involve an
  12. REM     increased degree of complexity in their definition and use.
  13. REM     This template aims to reduce the learning curve required and make macro's more accessible
  14. REM     As such, no processing of the Arguments provided to the Macro is performed
  15. REM    
  16. REM     Any Macro specific processing is to be defined by You, the user, where the template notes
  17. REM     to insert such processing.
  18. REM    
  19. REM     The DEFAULT behavior is to split the arguments into an Array defined to $Return[#], where
  20. REM     # is the current index of the Array (1 indexed).
  21. REM     The size of the Array is returned in $Return[i]
  22. REM     $Return may be substituted using Substring Modification with a custom variable name at the
  23. REM     time of expansion.
  24. REM    
  25. REM    Use the search and replace tool of your script editor to replace $Macro.Template with the
  26. REM    intended name of your Macro
  27.  
  28. REM Recommended Learning resources:
  29.     REM [1] https://www.dostips.com/forum/viewtopic.php?t=9265#p60294
  30.     REM [2] https://www.dostips.com/forum/viewtopic.php?f=3&t=10983&sid=f6937e02068d93bc5a97ef63d4e5319e
  31.     REM  - Macros with arguments:
  32.     REM [3] https://www.dostips.com/forum/viewtopic.php?f=3&t=1827
  33.     REM  - CMD parsing behaviour:
  34.     REM [4] https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts
  35.  
  36.     REM String length method is a modified version originally produced by JEB
  37.     REM See link at resource [2] above.
  38.  
  39. (Set $\n=^^^
  40.  
  41. %= DO NOT MODIFY $\n newline variable for multi-line macro definition =%)
  42.  
  43. REM - Each internal line of multiline macro's must be terminated with the escaped linefeed variable:
  44. REM   %$\n%
  45.  
  46. Set ^"$DQ=""
  47.    
  48. REM - EnableDelayedExpansion Aka EDE required to %expand% macro/s
  49. REM   May be placed before or after Macro definiton.
  50. Setlocal EnableDelayedExpansion
  51.  
  52. For /f %%! in ("! ^! ^^^!") Do %=                                     This outer loop allows DDE or EDE environment independant definition =%^
  53. Set $Macro.Template=For %%n in (1 2)Do if %%n == 2 (            %$\n:  IMPORTANT  - No whitespace permitted at Start of this Line  =%
  54.     If "%%!%%!" == "" (                             %$\n:  DDE or EDE test.  If enableDE; enact macro  =%
  55.         If not "%%!$Macro.Template.Args%%!" == "" (                 %!! Args do arg processing =%
  56.             If "%%!$Macro.Template.Args:~0,1%%!" == " " Set "$Macro.Template.Args=%%!$Macro.Template.Args:~1%%!"        %$\n%
  57.             For /f "delims=" %%Q In ("%%!$DQ%%!")Do (                                   %$\n%
  58.                 Set "$Macro.Template.Args=%%!$Macro.Template.Args: %%Q=,%%Q%%!"                     %$\n%
  59.                 Set "$Macro.Template.Args=%%!$Macro.Template.Args:%%Q =%%Q,%%!"                     %$\n%
  60.             )                                                       %$\n%
  61.             If not "%%!$Macro.Template.Switch[Defined]%%!" == "" (                              %$\n%
  62.                 For /f "tokens=1 Delims==" %%G in ('Set "$Macro.Template.Switch"')Do Set "%%G="             %$\n%
  63.             )                                                       %$\n%
  64.             Set "$Macro.Template.Arg[i]=0"                                          %$\n%
  65.             Set "$Macro.Template.Args=%%!$Macro.Template.Args: /=,/%%!"                         %$\n%
  66.             For /l %%i in (1 1 750)Do If defined $Macro.Template.Args (                         %$\n%
  67.                 For /f "tokens=1 Delims=," %%? in ("%%!$Macro.Template.Args%%!")Do If not %%? == "" (           %$\n%
  68.                     Set "$Macro.Template.ThisArg=%%~?"                              %$\n%
  69.                     (Set^^ "$_=%%~?")                                       %$\n%
  70.                     If Defined $_ (                                         %$\n%
  71.                         Set "$Len=1"                                        %$\n%
  72.                         For %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1)Do (                %$\n%
  73.                             If "%%!$_:~%%P,1%%!" NEQ "" (                           %$\n%
  74.                                 Set /a "$Len+=%%P"                          %$\n%
  75.                                 Set "$_=%%!$_:~%%P%%!"                          %$\n%
  76.                             )                                       %$\n%
  77.                         )                                           %$\n%
  78.                     )Else set "$Len=0"                                      %$\n%
  79.                     Set "$Macro.Template.Switch="                                   %$\n%
  80.                     Set /A "$Len.OG=%%!$Len%%!","$Macro.Template.Argcull=3"                     %$\n%
  81.                     If "%%?" == "%%~?" (                                        %$\n%
  82.                         Set "$Macro.Template.Argcull=1"                             %$\n%
  83.                     )                                               %$\n%
  84.                     If "%%!$Macro.Template.Argcull%%!" == "1" (                         %$\n%
  85.                         IF "%%!$Macro.Template.ThisArg:~0,1%%!" == "/" (                    %$\n%
  86.                             Set "$Macro.Template.Switch=true"                       %$\n%
  87.                             Set "$Macro.Template.Switch[Defined]=true"                  %$\n%
  88.                             Set "$Macro.Template.Switch[%%!$Macro.Template.ThisArg:~1,1%%!]=true"       %$\n%
  89.                             If %%!$Len%%! GTR 2 (                               %$\n%
  90.                                 Set "$Macro.Template.Switch[%%!$Macro.Template.ThisArg:~1,1%%!]=%%!$Macro.Template.ThisArg:~3%%!" %$\n%
  91. %$\n:_____________________________________________________ TOGGLE SWITCH OFF /[A-Z]- ______________________________________________________=%
  92.                                 IF "%%!$Macro.Template.ThisArg:~3%%!" == "-" Set "$Macro.Template.Switch[%%!$Macro.Template.ThisArg:~1,1%%!]="      %$\n%
  93.                             )%$\n%
  94.                             Set /a "$Len+=$Macro.Template.Argcull"                      %$\n%
  95.                             For /F "delims=" %%i in ("%%!$Len%%!")Do (                  %$\n%
  96.                                 Set "$Macro.Template.Args=%%!$Macro.Template.Args:~%%i%%!" %$\n:Shift Arg =%
  97.                             )                                       %$\n%
  98.                         )                                           %$\n%
  99.                     )                                               %$\n%
  100.                     If "%%!$Macro.Template.Switch%%!" == "" (                   %$\n:    Is Arg    =%
  101.                         Set /A "$Macro.Template.Arg[i]+=1"%$\n%
  102. %$\n:________ IF THE ARGUMENTS STRING LENGTH IS REQUIRED FOR YOUR MACRO FUNCTION USE IT BEFORE NEXT LINE. EXPAND WITH A FOR LOOP __________=%
  103.                         Set /a "$Len+=$Macro.Template.Argcull"                          %$\n%
  104.                         For /F "delims=" %%i in ("%%!$Len%%!")Do (                      %$\n%
  105.                             Set "$Macro.Template.Args=%%!$Macro.Template.Args:~%%i%%!"  %$\n:  Shift Arg   =%
  106.                         )                                           %$\n%
  107. %$\n:______________________________________________ EXAMPLES OF BASIC SWITCH IMPLEMENTATION _______________________________________________=%
  108.         %= /o - output =%       If not "%%!$Macro.Template.Switch[o]%%!" == "" (Echo(%%!$Macro.Template.ThisArg%%!) %$\n%
  109.         %= /L - Return Length =%    If not "%%!$Macro.Template.Switch[L]%%!" == "" (                    %$\n%%
  110.                             Set "$Return[%%!$Macro.Template.Arg[i]%%!].Len=%%!$Len.OG%%!"           %$\n%
  111.                         )                                           %$\n%
  112. %!!%
  113.                         Set "$Return[%%!$Macro.Template.Arg[i]%%!]=%%!$Macro.Template.ThisArg%%!"       %$\n%
  114.                         Set "$Return[i]=%%!$Macro.Template.Arg[i]%%!"                       %$\n%
  115.                     )                                               %$\n%
  116.                 )                                                   %$\n%
  117.             )                                                       %$\n%
  118.         )Else (                                     %!!%
  119.             ^>"%~f0:$Macro.Template.help" (                                         %$\n%
  120.                 Echo(                                                   %$\n%
  121.                 Echo(Usage: Expand macro with double quoted arguments:                          %$\n%
  122.                 Echo( %%$Macro.Template%% [[/{A-Z}]:[{-}/{SwitchArg}]] "arg string" "arg string"            %$\n%
  123.                 Echo( - Or -                                                %$\n%
  124.                 Echo( %%$Macro.Template%% [[/{A-Z}]:[{-}/{SwitchArg}]] "arg string","arg string"            %$\n%
  125.                 Echo(                                                   %$\n%
  126.                 Echo( %%$Macro.Template:$Return=ReturnName%% "arg string","arg string","arg String"         %$\n%
  127.                 Echo( Use Substitution to provide a custom return variable name by replacing $Return            %$\n%
  128.                 Echo(                                                   %$\n%
  129.                 Echo( - Arguments must be double quoted: "argument"                         %$\n%
  130.                 Echo( - Arguments must not contain ',' characters.                          %$\n%
  131.                 Echo(   Comma: ',' is used internally to seperate arguments.                        %$\n%
  132.                 Echo( - Arguments may be seperated by either whitespace: " " or Comma: ","              %$\n%
  133.                 Echo(                                                   %$\n%
  134.                 Echo( * Switch states will be the currently set state for each subsequent Arg *             %$\n%
  135.                 Echo(   Use conditional testing within the macro to implement switch behavours.             %$\n%
  136.                 Echo(   - Where 'SwitchChar' is a single character. IE [A-Z]:                       %$\n%
  137.                 Echo( /SwitchChar       = Set state "$Macro.Template.Switch[SwitchChar]=true"               %$\n%
  138.                 Echo( /SwitchChar:-     = Set state "$Macro.Template.Switch[SwitchChar]="               %$\n%
  139.                 Echo( /SwitchChar:value = Set state "$Macro.Template.Switch[SwitchChar]=value"              %$\n%
  140.                 Echo(                                                   %$\n%
  141.                 Echo( Switch usage example                                      %$\n%
  142.                 Echo( %%$Macro.Template%% "arg 1" "arg 2" /o "Arg 3"                            %$\n%
  143.                 Echo(   would output only the 3rd argument and beyond                           %$\n%
  144. %!!%
  145.                 Echo(                                                   %$\n%
  146.             )                                                       %$\n%
  147.             (MORE /C ^<"%~f0:$Macro.Template.help")                                     %$\n%
  148.         )                                                           %$\n%
  149.     )Else (                                         %$\n:  NO EDE - Notify Environment Req.    =%
  150.         Echo(                                                           %$\n%
  151.         Echo( %%!   Delayed Expansion is currently Disabled.                                %$\n%
  152.         Echo(   To successfully expand %%$Macro.Template%%, the command:                            %$\n%
  153.         Echo(                                                           %$\n%
  154.         Echo(Setlocal EnableDelayedExpansion                                            %$\n%
  155.         Echo(                                                           %$\n%
  156.         Echo(   is required *before* this macro command.                                    %$\n%
  157.         Echo(                                                           %$\n%
  158.     )                                                               %$\n%
  159. )Else Set $Macro.Template.Args=%= Capture macro Args =%
  160.  
  161. REM Strip TABS from macro definition if in an EDE environment to conserve environment space.
  162. If "!!" == "" Set "$Macro.Template=!$macro.Template:    =!"
  163.  
  164. Echo(Example:
  165. Echo(   %%$Macro.Template:$Return=My Array%% "Arg one" "Arg two" "<a=b>" "this=?" /c SwitchArg "and*" /o /l "Hello World^^^!" /o:- "." un quoted, /l:-,non switch UQ arg
  166. Echo(
  167.       %$Macro.Template:$Return=My Array% "Arg one" "Arg two" "<a=b>" "this=?" /c SwitchArg "and*" /o /l "Hello World^^^!" /o:- "." un quoted,non switch UQ arg
  168. Echo(
  169. Set "My Array"
  170. Set "$Macro.Template.Switch"
  171. Echo(
  172. PAUSE & 1> nul TIMEOUT /T 1 /NOBREAK
  173.  
  174. REM Usage displayed if expanded with no args
  175. %$Macro.Template%
  176.  
  177. Setlocal DisableDelayedExpansion
  178. %$Macro.Template% "Demo Macro Error output in Environment Without EnableDelayedExpansion"
  179.  
  180. (
  181.     Endlocal & Endlocal & Endlocal
  182.     Pause
  183. )
Advertisement
Comments
  • T3RRYT3RR0R
    95 days
    # text 0.53 KB | 0 0
    1. Template updated 12/02/2024 to include switch processing and example switch handling.
    2.  
    3. Switches allow complex processing behavior to be applied to selected arguments within the same expansion of the macro function.
    4. Switch states are defined whe the switch as used IE: /S and can be disabled via the argument '-' IE: /S:-
    5.  
    6. Switches may also be provided unquoted argument values to be used to apply processing to subsequent arguments until or unless the switch state is again disabled - ensure supplied switch arguments are escaped if needed.
    7.  
Add Comment
Please, Sign In to add comment
Advertisement