Advertisement
T3RRYT3RR0R

Batch Linked variable Extraction demo

Apr 4th, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.03 KB | None | 0 0
  1. REM Batch script demonstrating extraction of values from linked variables
  2. REM explanation of the concepts here: https://stackoverflow.com/a/61024682/12343998
  3.  
  4. @echo off
  5.  
  6. Set Zero=One
  7. Set One=Two
  8. Set Two=Three
  9. set Three=Four
  10. set Four=Five
  11. set Five=Six
  12. set Six=Seven
  13. set Seven=Eight
  14. set Eight=Nine
  15. Set Nine=Ten
  16. Set Ten=Eleven
  17. Set Eleven=Twelve
  18. Set Twelve=Thirteen. Last variable. Cannot Be reached from depth 0
  19.  
  20. Goto :main
  21.  
  22. REM Subroutine to Build Expansion String based on depth:
  23.  
  24. :extract <Starting Variable> < Call Depth Range 1 to 11 > <Variable to Assign>
  25.     Setlocal EnableDelayedExpansion
  26.     Set "Assign=%~1"
  27.     Set EXPcount=1
  28.     Set "CALLcount=%~2"
  29.     IF Not "!CALLcount!"=="0" Set /A EXPcount=( !CALLcount! * 4 ) - 1
  30.     For /L %%A in (1,1,!EXPcount!) do (Set "Assign=%%!Assign!%%")
  31.  
  32.     Setlocal DisableDelayedExpansion
  33.     Set "Assign=Set Assign=!Assign!"
  34.     (
  35.     Endlocal
  36.         Set "Assign=%Assign%"
  37.     )
  38.  
  39.     For /L %%B in (0,1,!CALLcount!) do (Set "Assign=CALL !Assign!")
  40.     !Assign! || (Echo String exceeds line length, No Modification. & Exit /B)
  41.     IF "!Assign!"==" " (Echo Variable Not Defined, No Modification. & Exit /B)
  42.     (
  43.     Endlocal
  44.         Set "%~3=%Assign%"
  45.         Exit /B
  46.     )
  47.  
  48.  
  49. REM Expand variable using relationship between Number of Calls and % Expansion Required to Parse variable
  50. :main
  51. Setlocal EnableDelayedExpansion
  52.  
  53. REM Define variable 'Depth Shortcut' to target specific depths
  54. For %%A in (0 1 2 4 8 16 32 64 128 256 512 1024) Do (
  55.     Set /A #+=1
  56.     Set Depth[!#!]=%%A
  57. )
  58.  
  59. REM iterate over all depths
  60. For /L %%E in (1,1,!#!) do (
  61.     Call :Extract Zero !Depth[%%E]! New
  62.     Echo(Start: Zero Depth: %%E, Extracted Value: !New!
  63. )
  64. ECHO.
  65. REM extract from selected depth levels
  66. For %%E in (5 8 11) do (
  67.     Call :Extract Zero !Depth[%%E]! New
  68.     Echo(Start: Zero Depth: %%E, Extracted Value: !New!
  69. )
  70. ECHO.
  71. REM extract from selected depth levels, From a different starting Variable
  72. For %%E in (5 6 7 8 9) do (
  73.     Call :Extract five !Depth[%%E]! New
  74.     Echo(Start: Five Depth: %%E, Extracted Value: !New!
  75. )
  76. Endlocal
  77. Echo(Example Complete
  78. Pause >nul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement