How to declare variables (define variables) in DOS _dos/bat

Source: Internet
Author: User
Tags arithmetic bitwise current time numeric numeric value

DOS definition variable dos and only environment variables can be used

SET [variable=[string]]

variable specifies the name of the environment variable.
string specifies a series of strings to assign to the variable.

To display the current environment variable, type SET with no parameters.

If the command extension is enabled, the SET changes as follows:

You can activate the SET command with only one variable, and the equal sign or value does not show all prefix matches
The value of all variables for the name that the SET command has used. For example:

SET P

All the variables that begin with the letter P are displayed.

If the variable name is not found in the current environment, the SET command puts the ERRORLEVEL
Set to 1.

The SET command does not allow variable names to contain equal signs.


Two new command-line switches were added to the SET command:

set/a expression
set/p Variable=[promptstring]

/A command-line switch specifies that the string to the right of the equal sign is an evaluated numeric expression. of the expression
The evaluator is simple and supports the following operations in descending priority order:

()-Group
! ~--Unary operator
*/%-arithmetic operator
+--Arithmetic operator
<< >>-Logical shift
&-Bitwise "and"
^-Bitwise "Different"
| -Bitwise OR
= *=/=%= + = =-Assignment
&= ^= |= <<= >>=
,-An expression separator

If you use any logical or FETCH operator, you need to use an expression string
The quotation marks are expanded. Any non-numeric string key in an expression as an environment variable
Names, the values of these environment variable names have been converted to numbers before use. If you specify
Names, the values of these environment variable names have been converted to numbers before use. If you specify
An environment variable name, but not defined in the current environment, the value is set to
Zero. This allows you to use environment variable values to do calculations without typing those% symbols
came to their value. If set/a is executed at the command line outside of the command script,
Then it displays the final value of the expression. The operator of the assignment in the assigned operator
An environment variable name is required on the left. In addition to hexadecimal has 0x prefix, octal
With a 0 prefix, the numeric value is 10 digits. So, 0x12 with 18 and 022
Same. Note that the octal formula may be confusing: 08 and 09 are invalid numbers,
Because 8 and 9 are not valid octal digits.

The/p command-line switch allows variable values to be set to a single line of input entered by the user. Read input
Displays the specified promptstring before the row. The promptstring can be empty.

environment variable substitution has been enhanced as follows:

%path:str1=str2%

Expands the PATH environment variable, substituting "str2" for Each "str1" in the extended result.
To effectively remove all "str1" from the extended result, "str2" can be empty.
"STR1" can begin with an asterisk; in this case, "str1" extends the result from the
The place where the rest of the str1 first appeared has always been matched.

You can also specify a substring for the extension.

%path:~10,5%

Expands the PATH environment variable and uses only the 11th in the extended result (partial
Shift 10) character starts with five characters. If no length is specified, the default
Value, which is the remainder of the variable value. If two digits (offset and length) are negative,
The number used is the environment variable value length plus the specified offset or length.

%path:~-10%

Extracts the last 10 characters of the PATH variable.

%path:~0,-2%

All characters of the PATH variable are extracted, except for the last two.

Finally added support for delaying the expansion of environment variables. This support is always supported by the default value
Deactivate, but can also go through CMD. EXE is enabled/deactivated with the/V command line switch.
See also CMD/?

Delays the environment, considering the current expansion restrictions encountered when reading a line of text
Variable expansion is useful, not a time to execute. The following example illustrates the direct
Problem with variable expansion:

Copy Code code as follows:

Set Var=before
If "%var%" = = "Before" (
Set Var=after
If "%var%" = "after" @echo if you are here, it worked
)

The message is not displayed because when the first if statement is read, the BOTH if statement
The%var% will be replaced; The reason is that it contains the style of if, if it is a
Compound statement. So, the IF in a compound statement is actually comparing "before" and
"After", the two will never be equal. Similarly, the following example will not achieve
Expected effect:

Copy Code code as follows:

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 simply
The LIST variable is set to the last file found. This is also because%list% in
When the For statement is read, it is only expanded once; and the LIST variable at that time
is empty. So the For loop we actually execute is:

For%i in (*) do set list=%i

This loop continues to set the LIST to the last file found.

Deferred environment variable expansion allows you to use a different character (exclamation point) in the execution
Time to expand the environment variable. If the deferred variable extension is enabled, you can
Examples are written as follows to achieve the desired effect:

Set Var=before
If "%var%" = = "Before" (
Set Var=after
If "! var! "=" after "@echo If you are here, it worked
)

Set list=
For%i in (*) do set list=! list! %i
Echo%list%

If the command extension is enabled, there are several dynamic environment variables that can be extended, but
Does not appear in the list of variables shown in SET. Each time the value of the variable is expanded,
These variable values are computed dynamically. If the user uses any of these names
A defined variable that replaces the dynamic definition described below:

%cd%-Expands to the current directory string.


Set list=
For%i in (*) do set list=! list! %i
Echo%list%

If the command extension is enabled, there are several dynamic environment variables that can be extended, but
Does not appear in the list of variables shown in SET. Each time the value of the variable is expanded,
These variable values are computed dynamically. If the user uses any of these names
A defined variable that replaces the dynamic definition described below:

%cd%-Expands to the current directory string.

%date%-Expands to the current date in the same format as the date command.
%time%-Expands to the current time in the same format as the Times command.
%random%-Expands to an arbitrary decimal number between 0 and 32767.
%errorlevel%-Expands to the current ERRORLEVEL value.
%cmdextversion%-Expands to the current command processor extension version number.
%cmdcmdline%-Extends to the original command line that invokes the command processor.

Attach a sample code to judge:

Copy Code code as follows:

for/f%%i in (' dir/b c:\index2.htm ') do (
Set Indexdx=%%~zi
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.