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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

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

#1 Post by pstein » 06 Jun 2014 22:25

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

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#2 Post by ShadowThief » 07 Jun 2014 01:37

Image

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#3 Post by penpen » 07 Jun 2014 02:32

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

Post Reply