Variable Expansion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Variable Expansion

#1 Post by Squashman » 14 Nov 2016 15:15

This question stems from a question over on SO.

The user essentially has a SYSTEM environmental variable defined as go=go.exe %~n0. The user was then trying to use that as a MACRO variable in a batch file.

Now the first thing that comes to mind is that you would need to use CALL ECHO %go% to get the variables to expand correctly. Sure enough you see the expansion of %go% and %~n0 when you watch the code execute.

But what baffles me is if you just use CALL %go%, you never see the %~n0 expand but the program that is called does actually get the name of the batch file as its command line argument.

To test this I set my system variable as go=find.exe "%~n0" %0

I then ran this batch file named go.bat

Code: Select all

@echo on
setlocal
call %go%
endlocal
pause


And the verbose output is:

Code: Select all

C:\BatchFiles\>setlocal

C:\BatchFiles>call find.exe "%~n0" %0

---------- GO.BAT
call %go%

C:\BatchFiles>endlocal

C:\BatchFiles>pause
Press any key to continue . . .


So I am completely baffled as to how the find command is seeing my search parameter and the name of my batch file as the input file name.

What is CMD.EXE doing?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Variable Expansion

#2 Post by dbenham » 14 Nov 2016 15:42

I don't see the source of confusion. Everything in your post is exactly as I would expect, based on the batch parsing phases that jeb has outlined.

The %GO% variable is expanded in phase 1, and the result of the phases 1 and 2 are displayed (echoed) in phase 3. The CALL expansion doesn't occur until phase 6, long after the ECHO phase.

In a similar fashion, ECHOed statements do not show the result of delayed expansion (phase 5) either.

FOR variable expansion is a special case. The ECHO of the entire statement does not show the FOR variable expansion in phase 4, as would be expected. But then individual iterations of the body of the loop are ECHOed showing the result of FOR variable expansion. Looking at jeb's posted rules, that might seem to contradict the rules, but jeb implies there is more to FOR and IF processing than what he has published. I would love to see the complete FOR, IF, and REM rules that jeb has worked out some day.


Dave Benham

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Variable Expansion

#3 Post by Squashman » 15 Nov 2016 07:51

Thanks Dave.
I guess my code example proves his theory on phase 6.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Variable Expansion

#4 Post by pieh-ejdsch » 15 Nov 2016 16:56

:| If you want to see what the commandLine does , you'll take a

Code: Select all

echo on
For %%i in (1) do prog param


The rules of expansion in
For Vars within !Vars! enabled delayedexpansion is same as
CmdLine with %Vars%.

Into a ForVar:
In disable delayedexpansion the !Varname! Will be the String: !VarName!
Same as non set Varname in enabledexpansion.

When Delayedexpansion enabled a filled Varname will expanded to VarContent within a ForVar.

Phil

Post Reply