DOS - SETLOCAL and ENDLOCAL

Card Puncher Data Processing

About

Begins localization of environment changes in a batch file.

Environment changes made after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings. When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script.

Syntax

If Command Extensions are enabled SETLOCAL batch command accepts optional arguments.

These settings last until the matching ENDLOCAL command, regardless of their setting prior to the SETLOCAL command.

extension

ENABLEEXTENSIONS / DISABLEEXTENSIONS: enable or disable command processor extensions. These arguments takes precedence over the CMD /E:ON or /E:OFF switches.

delayed expansion

ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION: enable or disable delayed environment variable expansion. These arguments takes precedence over the CMD /V:ON or /V:OFF switches.

The SETLOCAL command will set the ERRORLEVEL value if given an argument. It will be zero if one of the two valid arguments is given and one otherwise.

Example

You can use this in batch scripts to determine if the extensions are available, using the following technique:

VERIFY OTHER 2>nul
SETLOCAL enableExtensions enableDelayedExpansion
IF ERRORLEVEL 1 echo Cannot enable delayed expansion&& exit /b 1

This works because on old versions of CMD.EXE, SETLOCAL does NOT set the ERRORLEVEL value. The VERIFY command with a bad argument initializes the ERRORLEVEL value to a non-zero value.





Discover More
Card Puncher Data Processing
DOS - Delayed environment variable

From the set help. The current expansion happens when a text block is read, not when it is executed. Here below, you have the example of an immediate variable expansion with an if block The following...
Card Puncher Data Processing
Dos - (Batch) Script

The batch script is a text file with the extension .bat or .cmd that is interpreted by a batch interpreter. You can call a batch script by : writing its name in the source script: The script execution...
Card Puncher Data Processing
Dos - Command

A command is: a Dos command (See below). of an utility. The DOS command are extended with management tools located in C:\Windows\System32...



Share this page:
Follow us:
Task Runner