Many people do not set up environment variables, here give a detailed explanation!
Set [variable = [string]]]
Variable specifies the name of the environment variable. String Specifies a series of characters to be assigned to the variable.
********************************** Only Type SET and does not add parameters to display current environment variables.
******************************* If the command extension is enabled, SET will change as follows:
You can activate the set command with one variable, the equivalent or value does not display all the values of all the variables of the name of the name that the set command has been used. E.g:
Set P
Display all variables with letters P headers
******************************** If the variable name is not found in the current environment, the set command will put Errorlevel Set to 1.
****************************************** SET commands do not allow variable names to contain equal numbers.
Two new replacements have been added to the set command:
SET / A Expression Set / P Variable = [PromptString]
/ A Command Option Specifies the string on the right side of the equal sign to the evaluated digital expression. The expression evaluation device is simple and supports the following operations in the order of decreasing priority:
() - Group * /% - calculative operator - - Calculation Operator << >> - Logic Shift - Bit "and" ^ - Bit "Al | - Bit" or "= * = / = % = = - = - assignment & = ^ = | = << = >> =, - expression separator
********************************* If you use any logic or endless operator, you need Expression character is built with quotation marks. Any non-numeric string key in the expression as an environment variable name, the value of these environment variable names have been converted to numbers before use. If an environment variable name is specified, but not defined in the current environment, the value will be set zero. This allows you to use environment variable values without typing those% symbols to get their values. If set / a executes the command line outside the command script, it displays the final value of the expression. The assigned operator requires an environment variable name on the left side of the assigned operator. In addition to the hexadecimal 0x prefix, octal has a 0 prefix, the numeric value is a 10-in number. Therefore, 0x12 is the same as 18 and 022. Please note that the octa-based formula may be easy to mix: 08 and 09 are invalid numbers because 8 and 9 are not valid eight-in-bit digits.
The / P Command Option allows the variable value to set a row of entries entered. Before reading the input line, display the specified PromptString. Promptstring can be empty.
Environment variable replacement has been enhanced:
% PATH: STR1 = STR2%
The PATH environment variable is expanded, replaced by "Str2" instead of each "Str1" in the result. To delete all "Str1" from the extended result, "str2" can be empty. "STR1" can be headed in an asterisk; in this case, "STR1" will remain comparable to the first occurrence of the beginning of the extended result to the STR1 remaining part.
The sub-string can also be specified for the extension. % PATH: ~ 10, 5%
The PATH environment variable is expanded, and then only five characters starting from the 11th (offset 10) character in the extended result. If no length is specified, the default value is used, that is, the number of variable values. If both numbers (offset, and length) are negative, the number used is the value of the environment variable value plus the specified offset or length.
% PATH: ~ -10%
The last ten characters of the PATH variable will be extracted.
% PATH: ~ 0, -2%
All characters of the PATH variable will be extracted, except for the last two.
Finally added support for delay environment variable expansion. This support is always deactivated by default, but can also be enabled / deactivated by cmd.exe / v command line command options. See cmd /?
Considering the current expansion of the current expansion when reading a line, the delay environment variable expansion is useful, not when executed. The following example explains the problem of expansion of direct variables:
Set var = before if "% var%" == "before" (set var = after; if "% var%" == "after" @echo if you see, IT worked)
The message will not be displayed because the% var% of the Both IF statement will be replaced when reading the first IF statement; the reason is: it contains the text of the IF, if the IF is a composite statement. Therefore, the IF in the composite statement is actually comparing "Before" and "After", both never wait. same. The following examples don't reach the expected results:
Set list = for% I in (*) do set list =% list%% i echo% list%
The reason is that it does not create a list of files in the current directory, but just set the LIST variable to the last file found. This is also because% List% is only expanded when the FOR statement is read; and the LIST variable at that time is empty. Therefore, the FOR cycle we really execute is:
For% i in (*) do set list =% i
This loop continues to set the list into the last file found.
Delay environment variable expansion allows you to expand environment variables in execution time using a different character (exclamation mark). If the delayed variable expansion is enabled, the above example can be written below to achieve the expected effect:
Set var = before if "% var%" == "before" (set var = after if "! var!" == "after" @echo if you see, IT worked)
Set list = for% I in (*) do set list =! list!% i echo% list%
********************************************** If command The extension is enabled, several dynamic environment variables can be extended, but do not appear in the list of variables displayed in the SET. These variable values are dynamically calculated when each variable value is expanded. If the user defines any variables in these names, that definition will replace the dynamic definitions described below:
% CD% - extended to the current directory string.
% DATE% - Extensions to the current date with the same format with the date command.
% Time% - Extension to the current time with the same format with the time command.
% Random% - extends to any decimal number between 0 and 32767. % ErrorLevel% - Extension to the current ErrorLevel value.
% Cmdextversion% - Extension to the current command processor extension version number.
% Cmdcmdline% - extended to the original command line of the call command processor.
*********************************************************** *****
End