existcmd
(Windows Command Prompt) script,@setlocal
It is a commonly used combination command, consisting of two parts:@
andsetlocal
。
- @: In the script,
@
Used to prevent the display of commands. Usually, every line of command is displayed when executing a batch file. When you prepend a line of commands@
, this line of command will not be displayed in the command prompt window. This is often used to make the output of scripts cleaner and tidy. - setlocal: This is a command that starts localizing the environment changes. It is usually used in batch files to ensure that any environment changes made in the script, such as setting or modifying environment variables, do not affect the context or other batch files that call this batch. When the script is executed, use
endlocal
The command can end this local environment and restore all environment settings tosetlocal
Previous status.
therefore,@setlocal
The combined purpose of is to localize the environment changes at the beginning of the batch file, while not displaying the command itself. This is a common practice when writing batch scripts to ensure that the script does not inadvertently modify the global environment settings.
1. Setlocal command
Command format
setlocal [ enableExtensions | disableExtensions ] [ enableDelayedExpansion | disableDelayedExpansion ]
Command function
The setlocal command is used to start localization of environment variables in batch files. Localization will continue until a matching endlocal command appears, or it will reach the end of the batch file.
【enableExtensions parameters】
Enabled command extension feature. extension means "expand, extend"
【disableExtensions parameters】
Closed command extension feature.
【enableDelayedExpansion parameters】
The variable delay expansion feature enabled. expansion means "extend, expand"
【disableDelayedExpansionn parameter】
Closed variable delay expansion feature.
【Notice】
Compared with the enableExtensions parameter and the disableExtensions parameter, they have higher priority.
The enableDelayedExpansion parameter and the disabledDelayedExpansion parameter have higher priority than the /V parameter.
2. Endlocal command
Command format:
endlocal
Command function:
The endlocal command ends the localization of environment variables in batch files.
【The role of "environmental variable localization"】
Implementing "environment variable localization" in batch files can avoid its "pollution" on the external operating environment
Example
The contents are as follows:
:: @echo off set var=200
The contents are as follows:
:: @echo off setlocal set var=200 endlocal
run
C:\>set var=5
C:\>demo1
C:\out>echo %var%
200
C:\>set var=5
C:\>demo2
C:\>echo %var%
5
C:\>
Summarize
The setlocal command and the endlocal command are valid in batch files (not valid in the dos command line environment)
The setlocal command executes all previous variable states (for example: variable values), and will be restored after the endlocal command (or batch file) is executed.
Between setlocal and endlocal (or at the end of the batch file), if a new variable is defined, the variable will be deleted after executing the endlocal command (i.e., its value is empty)
Between setlocal and endlocal (or at the end of the batch file), you can use the setlocal command to execute all previous variables (including variables defined by the external dos command line environment)
Both the setlocal command without parameters and the setlocal command with parameters (for example: setlocal enabledelayedexpansion) comply with the above rules.
The setlocal command with no parameters only localizes the modifications made by variables and does not affect the command extension features and variable delay expansion features of its scope (local and endlocal (or the local space between setlocal and endlocal (or at the end of the batch file).
When writing batch scripts, in order not to "pollute" the external running environment and to "default settings" your own running environment, you should use the setlocal command (and endlocal command) in the batch script. The following is a more commonly used "batch script template"
:: @echo off setlocal enableExtensions :: your code ... endlocal
Writing is not easy, if you find it useful to you