FOR variable %%A in macro definition gets overridden when script is called by other script within FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Maylow
Posts: 21
Joined: 15 Sep 2019 05:33

Re: FOR variable %%A in macro definition gets overridden when script is called by other script within FOR loop

#16 Post by Maylow » 02 Aug 2020 15:26

Success! Thanks to Jeb for the clear explanation and solution provided :D

I adjusted the code in the examples and got results as expected in all situations.

callee.cmd:

Code: Select all

@echo off
set "$PERCENT=%%%%~$=_p1_FOR-variable_DOLLAR_is_required_while_MACRO_DEFINITION_=:$"

FOR %%$ in (DOLLAR) DO (
    set "__macro= do (if /i "%$PERCENT%~A" equ "_some value" (echo("some value" detected) else (echo(incorrect value detected))"
)

%__call%("_%*") %__macro%
exit /b
caller_directly.cmd and caller_FOR.cmd remain unchanged.

When executing i get results as expected:

>callee some value
"some value" detected

>callee other value
incorrect value detected

>caller_directly.cmd some value
"some value" detected

>caller_directly.cmd other value
incorrect value detected

>caller_FOR.cmd some value
"some value" detected

>caller_FOR.cmd other value
incorrect value detected


Many thanks all for the help and very quick responses! :D

Post Reply