Computer Hope

Microsoft => Microsoft DOS => Topic started by: roohi on September 18, 2008, 05:21:04 AM

Title: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 05:21:04 AM
How can i export the varibles (set inside setlocal ) outside of setlocal .

for exmaple

setlocal
set var1=20
endlocal

set var2=%var1%

now here var2 should be set as enviorment varible that is fixed till we are not close that window.
Title: Re: export local variable outside of setlocal
Post by: Sidewinder on September 18, 2008, 06:27:21 AM
Quote
How can i export the varibles (set inside setlocal ) outside of setlocal

Why would you want to? What are you trying to do that would require localizing the variable and then attempting global access?

The setlocal statement is purposely designed to localize the variables.

The normal scope of a batch file variable is the duration of the session window (cmd/command). Setlocal reduces that scope until either an endlocal statement is encountered or the batch file ends, whichever comes first.

Is this related to this thread (http://www.computerhope.com/forum/index.php/topic,65955.0/all.html)?

 8)

Title: Re: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 06:32:17 AM
yes it is related to this thread.

so there is no way to access globally the local variable ?
Title: Re: export local variable outside of setlocal
Post by: Sidewinder on September 18, 2008, 06:49:04 AM
Quote
so there is no way to access globally the local variable ?

There seems to be some confusion about a variable and it's value. While a setlocal variable will die with an endlocal or the end of the batch file, it's value can live on through the file system.

Code: [Select]
@echo off
setlocal
echo 20 > var1
endlocal
set /p var2=<var1
echo %var2%
del var1

Not for nothing, but in your last thread we eliminated the setlocal statement. What happened?

 8)
Title: Re: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 06:56:00 AM
actually there is many variables are going to set as enviorment variable if i remove set local
so i have to use set local without delayexpanssion only for that part of code and i want to use that DIR value out side of that setlocal.

Now the issue is that when i echo varibles inside batch file it prints correct value of that varibles but when i tried to echo from commant prompt it self it does not show any value to varible .
here i m talking about the varibles set outside of setlocal with the help fo DIR.

r u understand what i want to say.
Title: Re: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 07:03:38 AM
lets explain u with the example

i set DIR inside setlocal

use DIR and set TOP_DIR after endlocal

When echo DIR and TOP_DIR it prints correct value

now after runing that batch file i run one makefile in same window and use TOP_DIR in that but it doesnot work.
Title: Re: export local variable outside of setlocal
Post by: Sidewinder on September 18, 2008, 07:18:59 AM
Please post the code you used.

Quote
i set DIR inside setlocal

use DIR and set TOP_DIR after endlocal

Quote
When echo DIR and TOP_DIR it prints correct value

I'm interested to see this as it directly contradicts the purpose of setlocal.

 8)

Using command names as your variable names is not good practice. It can only lead to confusion.
Title: Re: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 07:45:51 AM
Code: [Select]
SETLOCAL
set VOB_BASE_DIR=

for /f "tokens=* delims=\" %%P in ('cd') do (
set _mypath=%%P
)
set _array=%_mypath:\= %

for %%E in (%_array%) do (
if .%%E==. goto getout
  if %%E==test goto getout
  call set VOB_BASE_DIR=%%VOB_BASE_DIR%%\%%E
)
echo vob= %VOB_BASE_DIR%
ENDLOCAL
:getout
set VOB_BASE_DIR=%VOB_BASE_DIR:~1%

set TOP_DIR=%VOB_BASE_DIR%\\oscl
echo Set TOP_DIR to %TOP_DIR% ...


now after that i run makefile and it returns an error 
make:  makefile:  line 1:  Error -- Include file /makefile.mk, not found
and the first line of make is
Code: [Select]
include $(TOP_DIR)/makefile.mk
after that i tried to echo TOP_DIR from command prompt so it prints: %TOP_DIR%
it does not show value.
Title: Re: export local variable outside of setlocal
Post by: Sidewinder on September 18, 2008, 08:22:21 AM
I hate to repeat myself, but why are you even using the setlocal and it's evil twin endlocal statements? The whole point of the other thread was to not localize the variables so you would have global access.

Code: [Select]
set VOB_BASE_DIR=

for /f "tokens=* delims=\" %%P in ('cd') do (
set _mypath=%%P
)
set _array=%_mypath:\= %

for %%E in (%_array%) do (
if .%%E==. goto getout
  if %%E==test goto getout
  call set VOB_BASE_DIR=%%VOB_BASE_DIR%%\%%E
)
echo vob= %VOB_BASE_DIR%

:getout
set VOB_BASE_DIR=%VOB_BASE_DIR:~1%

set TOP_DIR=%VOB_BASE_DIR%\\oscl
echo Set TOP_DIR to %TOP_DIR% ...

Quote
When echo DIR and TOP_DIR it prints correct value

Quote
after that i tried to echo TOP_DIR from command prompt so it prints: %TOP_DIR%
it does not show value.
<sigh>

Assuming the syntax is correct, the makefile probably couldn't find a variable TOP_DIR

 8)

There are only 13 or so batch files commands. You don't need to use all of them in any single batch file.
Title: Re: export local variable outside of setlocal
Post by: roohi on September 18, 2008, 11:49:01 PM
thanks

Finally I understand that I have not to use set local .
Title: Re: export local variable outside of setlocal
Post by: vitt on April 10, 2010, 12:51:15 PM
Solution:

rem === Export %params% from setlocal scope
for /f "delims=" %%i in ('echo %params%') do endlocal & set params=%%i