====== Variables ====== Plugin operates with two types of variables --- internal and environmental variables. The only difference between them is only that the internal variables are only visible in the plugin, and the environment variables are available for Total Commander and programs, that run from it. The name of an internal variable can only consist of Latin letters, numbers and the underscore character "_". The name of an internal variable cannot start with a number. Environment variable names can contain any characters (except for "="), but the values of such variables can only be read using the [[GetEnv]] function. In order for an environment variable to be read as an internal variable, its name must consist only of Latin letters, numbers and the underscore "_". :!: Environment variables are set only by the [[SetEnv]] command and cannot be written in any other way. As a consequence, they cannot serve as variables for loops, they cannot be assigned values in expressions, they cannot be used as a variable to return values from commands, and so on. ==== Variables Scope ==== Internal variables in Autorun have a scope --- global or local. The scope is set using the **Global** and **Local** keywords. Local variables are valid only inside functions, global --- throughout the script. Additionally, with the help of the **Const** keyword, an internal variable can be defined as a constant so that it cannot be changed after the initial definition. Global [Const] Var1 [= expr] [, Var2 [= expr] .. [, VarN [= expr]] Local [Const] Var1 [= expr] [, Var2 [= expr] .. [, VarN [= expr]] Examples: Global Const MYGLOBALCONST1 = 123, MYGLOBALCONST2 = 456 Func MyFunc() Local MyLocalVar EndFunc :!: Environment variables and internal global variables with the same name cannot exist at the same time. Autorun will not let you create a global variable with the name of an existing environment variable, and vice versa. It is also possible to define variables without first specifying a scope: Var1 = expr Var2 = expr If the keywords **Global** or **Local** are not specified, then the rules are as follows: - If there is a global variable with the same name, then assignment within the function will change the value of this variable. After exiting the function, the changed value will be available for subsequent operations. - If it doesn't exist, then a local variable is created. var1 = 1 var2 = 1 MyFunc() Func MyFunc() Local var1 = 123 var2 = 123 Global var3 = 123 var4 = 123 Local var5 = 123 EndFunc var1 --> 1 var2 --> 123 var3 --> 123 var4 --> undefined var5 --> undefined Be careful when using undeclared variables in functions. If you plan to use a variable only within a function, it is recommended to always explicitly declare it in a local range to avoid possible errors. ==== Using Variables in Command Syntax ==== Internal variables are set with the [[Set]] command, and environment variables --- with [[SetEnv]]. When using variables in commands, Autorun does not make a fundamental difference between them and can get their values from either one or the other, however, writing from commands is only possible in internal variables (the obvious exception is SetEnv). When you use variables in the command follow a simple rule: if the value is reading ("expanding"), the variable must be wrapped around with percent "%". If the variable is writing (commands Set, SetEnv, and others), its name is written as is. Example: Set MY_COMMANDER_PATH %COMMANDER_PATH% In the above example, the environment variable COMMANDER_PATH will be read and then written to the variable MY_COMMANDER_PATH.