Batch file macro and SNAKE.BAT
Posted: 31 Dec 2019 02:19
I am new to batch macros and researching topics here in DosTips. I am aware of Macros with parameters appended and it's awesome, but I am specifically trying to implement macros that have no parameters, just manipulate variables and/or echo something, etc. Shown below is my test:
My question is regarding the macros in Dave Benham's SNAKE.BAT. Why does this work well, while macro1 and macro2 do not? Sample snippet from SNAKE.BAT (arrows added by me):
Thanks and happy new year everyone!
Meerkat
Code: Select all
@echo off
setlocal disabledelayedexpansion
(set \n=^^^
%== this creates escaped line feed for macro ==%
)
set macro0=set "x=1"^
^& set "y=2"
set macro1=set "x=1" %\n%
set "y=2"
set macro2=%\n%
set "x=1"%\n%
set "y=2"
set macro3=(%\n%
set "x=1" %\n%
set "y=2" %\n%
)
setlocal enabledelayedexpansion
set x=&set y=
%macro0%
echo(macro0 results: x="%x%";y="%y%"
set x=&set y=
%macro1%
echo(macro1 results: x="%x%";y="%y%"
set x=&set y=
%macro2%
echo(macro2 results: x="%x%";y="%y%"
set x=&set y=
%macro3%
echo(macro3 results: x="%x%";y="%y%"
macro0 is the earliest I found here in the forum, and works well. macro1 and macro2 are pitfalls: I think only the same line as "set macro=" is truly executed. My solution is to surround the macro with parentheses, as with macro3.Command Prompt wrote:macro0 results: x="1";y="2"
macro1 results: x="1";y=""
macro2 results: x="";y=""
macro3 results: x="1";y="2"
My question is regarding the macros in Dave Benham's SNAKE.BAT. Why does this work well, while macro1 and macro2 do not? Sample snippet from SNAKE.BAT (arrows added by me):
Code: Select all
:: draw
::: draws the board
set draw=%\n% <------
set screen=%\n% <------
for /l %%Y in (0,1,!height!) do set screen=!screen!!line%%Y!!LF!%\n%
...
Meerkat