Page 1 of 1

How to define "global" variables? Valid also for :proc?

Posted: 06 Jun 2014 22:25
by pstein
Assume I have a (simplified) DOS batch procedure like the one shown at the bottom.
Much to my surprise I have to redefine/set the two variables pathtoxxx and flag in the calling AND in the called procedure.
When I omit the second redefinition in the :proc the script won't run.

Is there really no other way to define variables globally?

Peter


@echo off &setlocal DisableDelayedExpansion
setlocal EnableDelayedExpansion
set pathtoxxx=D:\tmp\xxx
set flag=55

for %%i in (!params!) do (
echo processing %pathtoxxx% and %flag%
set "file=%%~i"
call :proc
)

:proc
set pathtoxxx=D:\tmp\xxx
set flag=55
%pathtoxxx%\program.exe %flag% "%file%"
goto :eof

Re: How to define "global" variables? Valid also for :proc?

Posted: 07 Jun 2014 01:37
by ShadowThief
Image

It seems to work fine. But also, try using !pathtoxxx!\program.exe !flag! "!file!" in :proc instead.

Re: How to define "global" variables? Valid also for :proc?

Posted: 07 Jun 2014 02:32
by penpen
I assume there is an endlocal in the original (non simplified) batch program between the for loop and the ":proc" label (but this is not needed).
In addition there is missing a "goto :eof" /"exit [/b [n]]" instruction as in the simplified batch program,
so the procedure ":proc" is executed without calling it.

The last behavior can be seen on the Command Prompt window of ShadowThief:
The doubled "C:\Windows 55 "two"" line.

penpen