Advertisement
T3RRYT3RR0R

ADVANCED Dynamic For loop construction Macro

Jun 12th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.23 KB | None | 0 0
  1. @Echo off
  2. ::: { Macro Definition
  3. Setlocal DisabledelayedExpansion
  4.     (set \n=^^^
  5. %= This creates an escaped Line Feed - DO NOT ALTER =%
  6. )
  7. ::: [ For Loop Constructor macro. ] For advanced programmers who need to use dynamic for loop options during code blocks.
  8. ::: - usage: %n.For%{For loop options}{variable set}{For Metavariable}{commands to execute}
  9. ::: - use delayed !and! variable to construct concatenated commands in the new for loop.
  10.     Set n.For=For %%n in (1 2) Do If %%n==2 (%\n%
  11.         Set FOR=%\n%
  12.         For /F "tokens=1,2,3,4 Delims={}" %%1 in ("!mac.in!") Do (%\n%
  13.             Set "FOR=For /F %%1 %%3 in ("!%%2!") Do (%%~4)"%\n%
  14.         )%\n%
  15.         Call :Exc.For%\n%
  16.     )Else Set mac.in=
  17.     Set "and.=&&"
  18.     Set "and=!and.!"
  19. ::: } End macro definition.
  20.     Setlocal EnableDelayedExpansion& rem // required to expand n.For constructor macro
  21. ::: - Usage examples:
  22.     Set "example=is a string line"
  23.     %n.For%{"tokens=* delims="}{example}{%%G}{Echo/%%~G}
  24.     %n.For%{"tokens=1,2,3,4 delims= "}{example}{%%G}{"Echo/%%~J %%~G %%~H %%~I !and! Echo/%%~I %%~G %%~H %%~J"}
  25.     Set "example2=Code block example"
  26.     For %%a in (1 2 3) do (
  27.         %n.For%{"Tokens=%%a Delims= "}{example2}{%%I}{"For /L %%# in (1 1 4) Do (Set %%I[%%#]=%%a%%#) !and! Set %%I[%%#]"}
  28.     )
  29.     Pause > Nul
  30.     Goto :EOF
  31. :Exc.For
  32.     %FOR%
  33. Exit /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement