Batch Programming Local Commands

How to Use SetLocal and EndLocal in Windows and MS-DOS Batch Files

© Guy Lecky-Thompson

Dec 19, 2007
A tutorial article using the setlocal and endlocal keywords in batch file programming under Windows and MS-DOS to set up environment variables that revert afterwards.

Batch file programming is a very useful way to automate small, repetitive, tasks. For those who are new to creating batch files, the Windows Command Line Programming tutorial is a good starting place to read about the basics of batch file programming.

When a batch file is executed, it can be made to do so within its own environment of local parameters. These include environment variables such as PATH which can be set within the context of the batch file (for convenience) and then allowed to revert afterwards, or upon request.

The SETLOCAL Command

The SETLOCAL command is used to specify that the batch script is about to make some changes to the various environment variables (such as PATH) which should be kept local to the current context. Any changes to PATH that is done between the SETLOCAL command and the end of the batch file will be temporary, and only applicable within the local context.

This includes the possibility to change the extension settings - using enableextensions and disableextensions - that are available. Assuming that they are supported under the command line being used, they will be turned on and off, respectively, for the local context.

The local context can be ended by using the ENDLOCAL command.

The ENDLOCAL Command

This command should be paired with a SETLOCAL command that appears before it, logically, within the batch script. There can be several SETLOCAL / ENDLOCAL pairs within a single batch file, but they can not be nested. In other words, there should never be two SETLOCAL commands executed one after the other.

The ENDLOCAL command can be used to terminate the local context before the end of the file is reached.

Testing for Command Extensions

Since the SETLOCAL command accepts the enableextensions parameter to turn command extensions on (with disableextensions providing the opposite effect), and returns an errorlevel result, it can be used to check for command extensions. Some implementations of a DOS-like operating system (like DR-DOS or FreeDOS) might not support extensions, which are enabled by default under Microsoft Windows XP and other command line implementations.

Note : under the MS-DOS command line, Microsoft stipulates that a nonzero value indicates an error.

Checking for extensions is a two step process:

  1. Pass an invalid command to set errorlevel to 1
  2. Use SETLOCAL enableextensions to reset errorlevel to 0

If, after carrying out the above, the errorlevel is still set to 1, then command extensions are not available. A good choice of a reasonably non-volatile command to use is the VERIFY command, that just verifies whether files are correctly written or not. So, using the following will cause errorlevel to be set to 1 because the parameter passed is invalid:

REM Send invalid output to nul to avoid screen echo
VERIFY banana 2>nul

After this, the SETLOCAL command can be used, followed by a test of the errorlevel, as detailed in the Batch File Programming IF Commands article.

Other Batch File Commands


The copyright of the article Batch Programming Local Commands in Command Line Programming is owned by Guy Lecky-Thompson. Permission to republish Batch Programming Local Commands in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo