Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: export local variable outside of setlocal  (Read 19024 times)

0 Members and 1 Guest are viewing this topic.

roohi

    Topic Starter


    Beginner

    export local variable outside of setlocal
    « 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.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: export local variable outside of setlocal
    « Reply #1 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?

     8)

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    roohi

      Topic Starter


      Beginner

      Re: export local variable outside of setlocal
      « Reply #2 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 ?

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: export local variable outside of setlocal
      « Reply #3 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)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      roohi

        Topic Starter


        Beginner

        Re: export local variable outside of setlocal
        « Reply #4 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.

        roohi

          Topic Starter


          Beginner

          Re: export local variable outside of setlocal
          « Reply #5 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.

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: export local variable outside of setlocal
          « Reply #6 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.
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          roohi

            Topic Starter


            Beginner

            Re: export local variable outside of setlocal
            « Reply #7 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.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: export local variable outside of setlocal
            « Reply #8 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.
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            roohi

              Topic Starter


              Beginner

              Re: export local variable outside of setlocal
              « Reply #9 on: September 18, 2008, 11:49:01 PM »
              thanks

              Finally I understand that I have not to use set local .

              vitt

              • Guest
              Re: export local variable outside of setlocal
              « Reply #10 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