DOS Batch Advanced Tutorial Fifth Chapter SET command detailed _dos/bat

Source: Internet
Author: User
Tags arithmetic bitwise goto numeric value

Set the custom variable with the SET command

Displays, sets, or deletes Cmd.exe environment variables.

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.

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

Note: The following usage clears the value of the variable variable so that it becomes undefined.

SET variable=

There are no symbols after the equals sign, and if you write set variable= "", the value of the variable is not NULL, but it is equal to two quotes, that is, "

Example:

@echo off
 Set var= I am the value
 echo%var%
 Pause

See Set Var= I'm a value, this is the way to set a variable directly in the batch!

Set is the command var is the variable name = number to the right "I am value" is the value of the variable

In the batch we will refer to this variant to enlarge the VAR variable name by two% (percent semicolon), such as%var%

Set can also provide an interactive interface, let the user input the value of the variable, and then we are to do the corresponding operation according to this value, now I would like to say that the syntax of set, only need to add a "/p" parameter on it!

set/p Variable=[promptstring]

Example:

@echo
off set/p var= Enter the value of the variable:
echo you entered%var% ~_~
pause

set/p is the command syntax var is the variable name = number to the right "Please enter the value of the variable:" This is the prompt, not the value of the variable!
After we run, we enter 1 directly after the prompt, and we will show you the line you entered 1 ~_~

Okay, let's go back to this, now. Set Other features

Use Set/? View Help for set we found the set in addition to what I said above
SET [variable=[string]]
set/p Variable=[promptstring]

In the case of these two syntaxes, there are several other syntaxes:
set/a expression

environment variable substitution has been enhanced as follows:
%path:str1=str2%
%path:~10,5%
%path:~-10%
%path:~0,-2%

What is the use of this machine grammar?

A simple calculation with set command

Syntax: set/a expression

/A command-line switch specifies that the string to the right of the equal sign is an evaluated numeric expression. The expression evaluator is simple and supports the following operations in descending order of precedence:
()-Group
! ~--Unary operator
*/%-arithmetic operator
+--Arithmetic operator
<< >>-binary logic shift
&-Binary bitwise "and"
^-Binary bitwise "Different"
| -Binary bitwise "OR"
= *=/=%= + = =-arithmetic Assignment
&= ^= |= <<= >>=-binary operation assignment
,-An expression separator

If set/a is executed at the command line outside of the command script, it displays the final value of the expression. Except for hexadecimal with a 0x prefix, the octal has a 0 prefix, with a numeric value of 10 digits. Therefore, 0x12 is the same as 18 and 022. Note that the octal formula may be confusing: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.

Above these are the system Help content, looked is not a little dizzy, does not matter I to explain briefly: Set's/A parameter is lets the set can support the mathematical symbol to carry on the addition subtraction and so on some mathematics operation!

Note: Normal operations are usually decimal operations, and if the number string is left to 0, it will be considered an octal error. For example, a number like 0812 can not participate in decimal operations, the conversion method is: 10812-10000

Cases:

Set aa=0812
set/a aa=1%aa%-10000
Echo%aa%

Results are: 812

Cases:

@echo off
set/p input= Please enter a calculation expression:
set/a var=%input%
echo evaluates:%input%=%var%
pause

The above example is the tornado design, very easy to use Yo, please see the following operation procedure:

Note: Dos computations can only be integer operations, accurate to integers

Please enter a calculation expression: 1+9+20+30-10
Calculated result: 1+9+20+30-10=50
Please press any key to continue ...

Please enter a calculation expression: 10/3 #除法只能精确到整数
Calculated result: 10/3=3
Please press any key to continue ...

Please enter a calculation expression: -100+62 #负数
Calculated result: -100+62=-38
Please press any key to continue ...

Please enter a calculation expression: 100%3 # to find the remainder
Calculated result: 100%3=1
Please press any key to continue ...
Note: The above remainder operator% must be written in the batch program as%

Please enter a calculation expression: (25+75) *2/(15+5) #括号
Calculated results: (25+75) *2/(15+5) =10
Please press any key to continue ...

Please enter a calculation expression: 1234567890*9876543210 #范围
Invalid number. The digital accuracy limit is 32 digits.
Calculated result: 1234567890*9876543210=
Please press any key to continue ...

Note: The above calculation process shows that the DOS calculation can only be accurate to 32 digits, this 32-bit refers to binary 32-bit, where the highest is the sign bit (0 is positive, 1 is negative), and the low 31 bit is the numeric value. 31 1 to decimal 2147483647, so the valid value range of DOS calculation is-2147483648 to 2147483647, when the value range exceeds the calculation error, see the following calculation procedure:

Please enter a calculation expression: 2147483647-1 #最大值减1, the value is valid
Calculated result: 2147483647-1=2147483646
Please press any key to continue ...

Please enter a calculation expression: 2147483647+1 #最大值加1, error, the result is a minimum value
Calculated result: 2147483647+1=-2147483648
Please press any key to continue ...

Please enter a calculation expression: -2147483648-1 #最小值减1, error, the result is the maximum value
Calculated result: -2147483648-1=2147483647
Please press any key to continue ...

Run set/a a=1+1,b=2+1,c=3+1 will display a 4, but we use the echo%a%%b%%c% After looking at the results, we will find that other mathematical operations also have effect!, this is the expression delimiter "tease" the role!

Sometimes we need to add and subtract directly from the original variable to use this syntax set/a var+=1 syntax corresponding to the original syntax is set/a var =%var% + 1
Are the same result, in the value of the original variable in the mathematical operation, but this simple to write one more:
set/a var*=2
Others are so used, as long as the help has this syntax!

There are also some use of logic or the operator, these symbols, according to the above method will be the error

For example, we enter set/a Var=1 & 1 "and operation" in Cmd, he does not display as 1, but the error, why? For such "logic or fetch operators," we need to enclose them in double quotes, or we can use the escape character ^, see examples

set/a var= 1 "&" 1 so the results are displayed, other logic or the use of the remainder operator
set/a var= 1 "+" 1 XOR operation
set/a var= 1 "%" 1 modulo operation
set/a var= 3 "<<" 2 left shift operation, 3 binary to 11, left 2 digits 1100, to decimal is 12, self-verification
set/a var= 4 ">>" 2 Right Shift operation, 4 binary to 100, right move 2 digits to 1, the result is 1

Tornado supplement: All in-position calculation needs to be converted into binary.

Study questions: Find 2 N-th
Reference Answer:

@echo off
set/p n= Please enter 2 of the several parties: 
set/a num=1^<^<n
echo%num%
Pause

Run Result:
Please enter 2 of the several Parties: 3
8
Please press any key to continue ...

Please enter 2 of the several parties: 10
1024
Please press any key to continue ...

Please enter 2 of the several parties: 15
32768
Please press any key to continue ...

Third, string processing with the SET command

1. String replacement

All right, here's the sign,%path:str1=str2%.
The above syntax means to replace the str1 in the string variable%path% with the STR2
This is the content of the substitution variable value, see example

@echo off
set a= bbs. Verybat. CN
Echo Replacement Value: "%a%"
set var=%a: =%
echo replaced value: "%var%"
Pause

Run Display: (Tornado added)
Value before replacement: "BBS." Verybat. cn
Value after replacement: "Bbs.verybat.cn"

In contrast, we found that he replaced the space of the variable%a%, and from this example we can find
%path:str1=str2% This operation is to replace all the str1 in the variable%path% with STR2.

For example, we change the above example to this

@echo off
set a=bbs.verybat.cn
echo Replace value: "%a%"
set var=%a:.= headache%
echo Replace value: "%var%"
Pause

Run the display:
Value before substitution: "bbs.verybat.cn"
After the replacement value: "BBS nerve-racking Verybat headache CN"

Explain set var=%a:.= headaches%
Set is the command var is the variable name A is the value of the variable to be replaced by the character, "." For the value to be replaced,
"Nerve-racking" for the replacement value!
When executed, the variable%a% the "." Inside. Replace all with "nerve-racking"
This is a good feature of the replacement character of set! Replace function First of all, here's the string intercept feature

Note that the substitution and interception of strings can be used where the variables are referenced, and the set command is not necessarily required
Cases:

@echo off
set a=bbs.verybat.cn
echo Replace value: "%a%"
echo Replace value: "%a:.= headache%"
pause

This example replaces the string in the Echo statement, and the effect is the same.

2. String interception

**********************************************
Intercepting functionality the unified syntax format is:%a:~[m[,n]]%
**********************************************
Square brackets indicate optional,% is variable identifier, a is variable name, not less, colon is used to separate variable name and description section, symbol ~ can be simply interpreted as "offset" can be, M is offset (the default is 0), n is the Intercept length (default is All)

%path:~10,5% What does this mean, look at the example:

Interception Function Example 1:

@echo off
set a=bbs.verybat.cn
set var=%a:~1,2%
echo%var%
Pause

After execution, we will find that only the "BS" two letters are displayed, and our variable%a% value is not for bbs.verybat.cn?
How to show only the 2nd letter and the 3rd letter "BS", the analysis of a result we can easily see that%path:~10,5% is displayed in the variable PATH from 11 bits (offset 10) Start 5 characters!
Analyze Set var=%a:~1,2%
Set is a command, VAR is a variable value, a variable to be manipulated by a character, "1" is displayed from the beginning of the variable "a", and "2" indicates several.
Together, we start with the value of variable a from 2nd (offset 1) and assign 2 characters to the variable var
This should be understood.

The other two kinds of syntax
%path:~-10%
%path:~0,-2%
They also show the meaning of the values specified by the specified variable

%path:~-10% See examples

Interception Function Example 2:

@echo off
set a=bbs.verybat.cn
set var=%a:~-3%
echo%var%
Pause

Run result:. cn
This is the value of the minus 3 digits of variable A to variable var

Of course, we can change that.
Interception Function Example 3:

@echo off
set a=bbs.verybat.cn
set var=%a:~3%
echo%var%
Pause

Run display:. verybat.cn
This is the whole value of variable A from the beginning of the 3rd bit to the variable var

%path:~0,-2% Example

Interception Function Example 4:

@echo off
set a=bbs.verybat.cn
set var=%a:~0,-3%
echo%var%
Pause

After implementation, we found that "Bbs.verybat" was shown, with less ". CN"
From the analysis of the results, it is easy to analyze that this is the value of the variable a starting from 0 bits,
The value between the last and third digits is all given to Var

If you change this

Interception Function Example 5:

@echo off
set a=bbs.verybat.cn
set var=%a:~2,-3%
echo%var%
Pause

Run Display: S.verybat
So he's just showing the minus three-bit value starting at 3rd bit (offset 2) and giving the variable var

Well done, examples are illustrative questions, for the convenience of memory, the Tornado section is as follows:

a=bbs.verybat.cn
%a:~1,2% = "BS" offset 1, 2 digits to the right from the second bit
%a:~-3% = ". CN" Offset negative 3, that is, the penultimate 3 digits (also understood to be left to the right 3 bits), right take all
%a:~3% = ". verybat.cn" Offset 3 (can also be understood to remove 3 digits to the left), all right
%a:~0,-3% = "Bbs.verybat" offset 0, right take length to minus 3, that is, the penultimate 3 digits
%a:~2,-3% = "S.verybat" Offset 2, right take length to minus 3, that is, the penultimate 3 digits

**********************************************
Therefore, the interception function Unified syntax format is:%a:~[m[,n]]%
**********************************************
Square brackets indicate optional,%a% is variable name, not less, colon is used to separate variable name and description section, symbol ~ can be simply interpreted as "offset", M is offset (default is 0), n is intercept length (default is All)
The usage described above is actually equivalent to the VBS function mid, left, right
%a:~0,n% is equivalent to left (a,n) to take n bit
%a:~-m% is equivalent to function right (A,M) take the right-hand m bit
%a:~m,n% equals function mid (a,m+1,n) takes n bits starting from m+1 bit
%a:~m,-n% corresponds to the function mid (A,m+1,len (a)-m-n), starting at the m+1 bit, to the reciprocal n+1 bit
%a:~m% corresponds to the function mid (A,m+1,len (a)-m) or right (A,len (a)-m), starting at the m+1 bit.

Thinking topic: Enter any string to find the length of the string
Reference Answer:

@echo off
set/p str= Please enter a string of any length:
echo You entered a string: "%str%"
call:stringlenth "%str%" num
echo string length:%num%
Pause
Exit

: Stringlenth
::---------string length Calculation subroutine
::---------parameter%1 is a string (if there are spaces, enclose in quotes):
:-- -------parameter%2 is the return variable name and cannot contain spaces or special characters
:: @echo off
set thestring=%~1
if not defined thestring goto:eof
Set return=0
: Stringlenth_continue
set/a return+=1
set thestring=%thestring:~0,-1%
if defined TheString Goto stringlenth_continue
if not "%2" = "" Set%2=%return%
goto:eof

If you want to learn more about the SET command, you can refer to the method below.

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.