Purpose:Restore the saved disk drive, directory, environment, local alias and function lists, and special characters, and exports selected variables

 

Format:ENDLOCAL [/D] [exportvar ...]

 

/D(ont restore)

 

See also: SETLOCAL.

 

Usage:

 

The SETLOCAL command saves the current disk drive, default directory, all environment variables, the alias and function lists, the directory stack (PUSHD), and the command separator, escape character, parameter character, decimal separator, and thousands separator. It does not save the user-defined function list or array variables. ENDLOCAL restores everything that was saved by the previous SETLOCAL command, except as described below.

 

If you have only global aliases and/or functions, SETLOCAL will copy them to a local list for the duration of the SETLOCAL. The matching ENDLOCAL will reset them to the global list. If you have both local and global aliases or functions, ENDLOCAL will only restore the local list (that was saved by SETLOCAL).

 

For example, this batch file fragment saves everything, removes all aliases so that user aliases will not affect batch file commands, changes the disk and directory, changes the command separator, runs a program, and then restores the original values:

 

setlocal

unalias *

cdd d:\test

setdos /c~

program ~ echo Done!

endlocal

 

SETLOCAL / ENDLOCAL may be nested within a single batch file up to 32 levels deep. You can also have multiple, separate SETLOCAL / ENDLOCAL pairs within a batch file, and nested batch files can each have their own SETLOCAL / ENDLOCAL. If you do not provide an ENDLOCAL in the batch file, TCC will do it automatically when the batch file exits.

 

You can also use SETLOCAL and ENDLOCAL in an alias, a library function, or at the command line. The maximum nesting level from a command line or alias is 32 levels. Unlike batch files, you are responsible for matching the SETLOCAL / ENDLOCAL calls from an alias or command line; TCC will not perform an automatic ENDLOCAL.

 

An ENDLOCAL is performed automatically at the end of a batch file, or when returning from a "GOSUB filename". If you invoke one batch file from another without using CALL, the first batch file is terminated, and an automatic ENDLOCAL is performed; the second batch file inherits the settings as they were prior to any SETLOCAL.

 

Exporting environment variables

 

The environment variables whose names are specified in the ENDLOCAL command are exported. This means that their names and values from inside the SETLOCAL / ENDLOCAL will be placed into the restored environment, either adding variables, or possibly modifying them. In the example below, the variable TEST will have the value abcd after the ENDLOCAL is executed, regardless of what its value was, or even if it had not been previously defined:

 

setlocal

set test=abcd

endlocal test

 

The list of variables to export may contain wildcards. All variables matching the requested pattern will be exported.

 

Exporting current working directory

 

See option /D below.

 

Options:

 

/D(Don't restore directory)  Export the current directory: the original drive and directory saved by SETLOCAL will not be restored.