I found that there are two types of environment variable, global variable and local variable, What is the difference between them? Are global and local variable same as system and user variable? Can the setlocal and endlocal commands prevent global variables from being set?
A global variable is a variable that can be read and written to at any point in the script. By default, all variables in batch are global strings.
A local variable exists inside of a scope that has a beginning defined by the setlocal
command and an ending defined by the endlocal
command (note that the end of a script has an implicit endlocal
if one is not present). If a variable is defined inside of this scope, it will not exist after the scope ends. If the variable was defined outside of this scope and its value gets changed inside of the scope, it will revert to its original value once the scope ends.
System variables are variables that get automatically set by the system upon startup. User variables are any variables that get created by humans. Again, everything is global by default.
There is no way to make a variable read-only.