You may see this code in a windows bat script. You may think this command enables command extensions and delayed variable expansion. But this command actually have 3 effects:
- set variable modifications local to this script, i.e., the change to variable value disappears after the script ends. The variables revert to their original values. Without setlocal, the changes of variables preserves even after the bat script exits. You can get the modified value in the cmd that runs the script.
- enable command extensions. What is command extensions? Well, command extensions are those switches of commands that cannot be used if command extensions is disabled. For example, the For command can have a /L switch which generates a list of numbers for the loop variable. If the command extensions is not enabled, you will get this error when running this command:/L was unexpected at this time.
Fortunately, command extensions is enabled by default. So theoretically, you do not need to enable it using setlocal ENABLEEXTENSIONS. Most bat scripts do use “setlocal ENABLEEXTENSIONS” to enable command extensions explicitly to make it clear and absolute. If you want to disable command extensions, you need to setlocal DISABLEEXTENSIONS. Note that if you use “setlocal DISABLEEXTENSIONS” in a script, the cmd process that runs the script is also affected, i.e., you cannot use command extensions in the cmd after the script executed. You can open a cmd window that disables command extensions by running “cmd /y“. Then FOR /L does not work in the cmd window, and in the scripts executed by that cmd.
- Enable delayed expansion of variable.