Loading ...
Sorry, an error occurred while loading the content.

Re: local / global variables

Expand Messages
  • John Zeman
    ... Garry by any chance did you mean a multi command line instead of a multi-line command? If not, I didn t follow you this time, and knowing you and your
    Message 1 of 4 , Apr 1 10:35 AM
    • 0 Attachment
      --- In batchworld@yahoogroups.com, "Garry Deane" <garrydeane@y...>
      wrote:

      > You can "export" local variables outside of the endlocal by
      > using a multi-line command.
      >
      > setlocal
      > md;2>nul
      > set localerr=%errorlevel%
      > ver>nul
      > echo %errorlevel%
      > endlocal & set localerr=%localerr%
      >
      > echo %localerr%
      >
      > Garry


      Garry by any chance did you mean a multi command line instead of a
      multi-line command? If not, I didn't follow you this time, and
      knowing you and your accuracy, I'm likely missing something.

      However after a bit of experimenting I've discovered a couple ways to
      preserve the value of a variable after an endlocal command. The
      following example is what I would normally expect after endlocal, the
      variable X would have the original value of "Goodbye" instead
      of "Hello".

      set X="Goodbye"
      setlocal
      set X="Hello"
      echo Within setlocal the value of %%X%% is: %X%
      endlocal
      set X=%X%
      echo After endlocal the value of %%X%% is: %X%

      The second example retains the value of "Hello" after endlocal

      set X="Goodbye"
      setlocal
      set X="Hello"
      echo Within setlocal the value of %%X%% is: %X%
      endlocal & set X=%X%
      echo After endlocal the value of %%X%% is: %X%

      As does this third example.

      set X="Goodbye"
      setlocal
      set X="Hello"
      echo Within setlocal the value of %%X%% is: %X%
      (endlocal
      set X=%X%)
      echo After endlocal the value of %%X%% is: %X%


      Yet another trick is learned, thanks Garry. :)

      John
    • Garry Deane
      ... Yes you re correct. I should have said multiple command line. I tend to (incorrectly) think of them interchangeably since grouping multiple commands
      Message 2 of 4 , Apr 1 4:19 PM
      • 0 Attachment
        --- In batchworld@yahoogroups.com, "John Zeman" <john041650@y...>
        wrote:
        > Garry by any chance did you mean a multi command line
        > instead of a multi-line command? If not, I didn't follow
        > you this time, and knowing you and your accuracy, I'm
        > likely missing something.
        <snip>

        Yes you're correct. I should have said multiple command line.

        I tend to (incorrectly) think of them interchangeably since
        grouping multiple commands over multiple lines with
        parentheses is equivalent e.g.

        (echo line 1
        echo line 2)

        is just a convenient way to write and is equivalent to:

        echo line 1&echo line 2

        Sorry for the confusion.

        Garry
      Your message has been successfully submitted and would be delivered to recipients shortly.