ENDLOCAL – Windows CMD Command


Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

End localization of environment changes in a batch file. Pass variables from one batch file to another.

If SETLOCAL is used to make variables ‘local’ to one batch script, then those variables will be invisible to all other batch scripts unless explicitly passed using an ENDLOCAL & SET… command.

If SETLOCAL is used without a corresponding ENDLOCAL then local environment variables will be discarded when the batch file ends.
In effect, the ENDLOCAL happens automatically when the script terminates.

Ending a cmd.exe session will discard all Environment Variables both local and global.

If a batch script does not use SETLOCAL then all variables will be Global, i.e. visible and modifiable from other calling scripts or on the command line after the script has completed.

Passing variables from one routine to another

The CMD command processor always works on a line-by-line basis, so it will convert all %variables% into their text values before executing any of the commands.

By putting ENDLOCAL & SET commands on a single line you are able to SET a variable just before the localization is ended by the ENDLOCAL command.

ENDLOCAL does not reset %errorlevel%

Multiple SET commands can be added to pass multiple variables, just prefix each with an &
Be aware that any trailing spaces will be added to the variable’s value.

Improving readability

The ‘ENDLOCAL & SET’ technique described above can become difficult to read if you have a lot of SET commands all on the same line. This can be made easier to read if you use parenthesis.

In these examples, we have used the variable names _return1, _return2, etc, and quoted all the assignments. You can of course use any names for the return variables, even re-use the exact same variable name inside and outside the ENDLOCAL command (SET _price=%_price%)

ENDLOCAL does not set or clear the Errorlevel.
ENDLOCAL is an internal command.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *