Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REM Batch script demonstrating extraction of values from linked variables
- REM explanation of the concepts here: https://stackoverflow.com/a/61024682/12343998
- @echo off
- Set Zero=One
- Set One=Two
- Set Two=Three
- set Three=Four
- set Four=Five
- set Five=Six
- set Six=Seven
- set Seven=Eight
- set Eight=Nine
- Set Nine=Ten
- Set Ten=Eleven
- Set Eleven=Twelve
- Set Twelve=Thirteen. Last variable. Cannot Be reached from depth 0
- Goto :main
- REM Subroutine to Build Expansion String based on depth:
- :extract <Starting Variable> < Call Depth Range 1 to 11 > <Variable to Assign>
- Setlocal EnableDelayedExpansion
- Set "Assign=%~1"
- Set EXPcount=1
- Set "CALLcount=%~2"
- IF Not "!CALLcount!"=="0" Set /A EXPcount=( !CALLcount! * 4 ) - 1
- For /L %%A in (1,1,!EXPcount!) do (Set "Assign=%%!Assign!%%")
- Setlocal DisableDelayedExpansion
- Set "Assign=Set Assign=!Assign!"
- (
- Endlocal
- Set "Assign=%Assign%"
- )
- For /L %%B in (0,1,!CALLcount!) do (Set "Assign=CALL !Assign!")
- !Assign! || (Echo String exceeds line length, No Modification. & Exit /B)
- IF "!Assign!"==" " (Echo Variable Not Defined, No Modification. & Exit /B)
- (
- Endlocal
- Set "%~3=%Assign%"
- Exit /B
- )
- REM Expand variable using relationship between Number of Calls and % Expansion Required to Parse variable
- :main
- Setlocal EnableDelayedExpansion
- REM Define variable 'Depth Shortcut' to target specific depths
- For %%A in (0 1 2 4 8 16 32 64 128 256 512 1024) Do (
- Set /A #+=1
- Set Depth[!#!]=%%A
- )
- REM iterate over all depths
- For /L %%E in (1,1,!#!) do (
- Call :Extract Zero !Depth[%%E]! New
- Echo(Start: Zero Depth: %%E, Extracted Value: !New!
- )
- ECHO.
- REM extract from selected depth levels
- For %%E in (5 8 11) do (
- Call :Extract Zero !Depth[%%E]! New
- Echo(Start: Zero Depth: %%E, Extracted Value: !New!
- )
- ECHO.
- REM extract from selected depth levels, From a different starting Variable
- For %%E in (5 6 7 8 9) do (
- Call :Extract five !Depth[%%E]! New
- Echo(Start: Five Depth: %%E, Extracted Value: !New!
- )
- Endlocal
- Echo(Example Complete
- Pause >nul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement