Batch SET command details let you understand set command 1th/2 page _dos/bat

Source: Internet
Author: User
Tags arithmetic bitwise goto cmd copy


Set,e translation is the meaning of "setting", which is equivalent to the "order" in mathematics.
such as: Set X=5, is to make x=5 meaning.
Grammatical form:
SET [variable=[string]]
set/p Variable=[promptstring]
set/a expression

One, SET [variable=[string]]

Example 1:
@echo off
Set
Pause
Display the values of all variables

Example 2:

@echo off
Set Var= I'm a value
Echo%var%
Pause
See Set Var= I'm a value, and that's the way bat sets variables directly in batch processing!
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 variable to extend the var variable name to two% (percent semicolon), such as%var%

Second, set/p variable=[promptstring]

Sometimes we need to provide an interactive interface, let the user input the value of the variable, and then we are here to do the corresponding operation, now I say this set of this syntax, only need to add a "/p" parameter on it!
Example 1:
@echo off
set/p var= Please enter your name:
Echo Your name is:%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 a hint, not a change
The value of the quantity!
After we run, we enter Robin directly after the prompt, and we will display a line of your "Your name is Robin"

Third, 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 parsing is simple and supports the following operations in descending order of precedence:
()-Group
! ~--Unary operator
*/%-arithmetic operator
+--Arithmetic operator
<< >>-Logical shift
&-Bitwise "and"
^-Bitwise "Different"
| -Bitwise OR
= *=/=%= + = = &= ^= |= <<=-Assignment
,-An expression separator
Set of the/a parameter is let set can support mathematical symbols to add and subtract some mathematical operations!
Let's start with an example to illustrate the use of these mathematical symbols:
See examples here for examples please directly under cmd Copy command run, do not need to save for bat!
set/a Var=1 + 1
set/a syntax, var variable name 1 + 1 math equation
The copy will display a 2 directly after it is run, or we can enter Echo%var% and two after the run, which is
A simple addition operation!

Copy Code code as follows:

set/a var=2-1 How much is the result? If you don't see the result, echo%var% ...
set/a var=2 * 2 multiplication operation
SET/A VAR=2/2 Division Operation
set/a var= (1+1) + (1+1) The result is equal to 4 read it!

Example 1:

Copy Code code as follows:

@echo off
set/a a=1+1,b=2+1,c=3+1
echo%a%%b%%c%


Example 2:

Copy Code code as follows:

@echo off
Set a=100
Set b=1000
set/a c=a+b
echo the result is:%c%
Pause

Sometimes we need to add and subtract directly from the original variable to use this syntax
set/a var+=1 This syntax corresponds 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 is easier to write
In the coming one:
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 show 1, but an error,
Why? For such "logic or the remainder operator," we need to enclose them in double quotes, see examples

Copy Code code as follows:

set/a var= 1 "&" 1 so the results are displayed, other logic or the use of the remainder operator
set/a var= 1 "^" 1 different operation
set/a var= 1 "%" 1 modulo operation
set/a var= 2 "<<" 2-bit left-shift
set/a var= 4 ">>" 2-bit right shift

Example 3:

Copy Code code as follows:

@echo off
Set a=100
Set b=1000
set/a c=a+b
set/a c= C "<<" 2
echo the result is:%c%
Pause

These symbols can also be used in &= ^= |= <<= >>= such simple usages as
set/a var "&=" 1 equals set/a var =%var% "&" 1 Note quotes
Common usage examples for set:
1. Set Display environment variable:
For example: At the command line, enter:
Set displays all current system environment variables
Set C displays all variables (i.e. case-insensitive) that begin with C or C.
Set COM displays all variables (case-insensitive) that start with COM or com;
2, set setting environment variable, usage example: set expression,
such as: Set Str=ok
is to set the value of the variable str to the string "OK"
Note: Set str=ok>
This error occurs during batch processing, and the system prompts: "The command syntax is incorrect." ”
Because the character ">" of the Value "ok>" in the variable str is a special character that will be handled by the system as a specifier, so there will be an error, and if you do set such a variable str, what about the value "ok>"? There are two solutions: the special character is escape, the Escape method is preceded by a "^", such as: Set Str=ok^> (this method is less useful, because there is not much application value)
A common workaround is to enclose the entire expression in quotation marks after set, such as: Set "str=>"
3. Set numerical calculation with parameter/a
Set of the/a parameter is let set can support mathematical symbols to add and subtract some mathematical operations! Supported mathematical operations include (decrement of priority):
()-Group
! ~--Unary operator
*/%-arithmetic operator
+--Arithmetic operator
<< >>-Logical shift
&-Bitwise "and"
^-Bitwise "Different"
| -Bitwise OR
= *=/=%= + = =-Assignment
&= ^= |= <<= >>=
,-An expression separator
Usage Example: set/a num=2*10, so that the value of the variable num changes to: 20

Copy Code code as follows:

set/a var=2-1 How much is the result? If you don't see the result, echo%var% ...
set/a var=2 * 2 multiplication operation
SET/A VAR=2/2 Division Operation
set/a var= (1+1) + (1+1) The result is equal to 4 read it!
SET/A A=1+1,b=2+1,c=3+1 Run will show a 4, but we use
Echo%a%%b%%c% After looking at the results, you will find that other mathematical operations also have effect!, this is the "dou" number.
Role!

Sometimes we need to add and subtract directly from the original variable to use this syntax
set/a var+=1 This syntax corresponds to the original syntax is set/a var =%var% + 1
is the same result.
For "logical OR fetch operators," we need to enclose them in double quotes, see examples
Copy Code code as follows:

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= 2 "<<" 2-second-order operation
set/a var= 4 ">>" 2 this doesn't quite remember the term in math ....

These symbols can also be used in &= ^= |= <<= >>= such simple usages as
set/a var "&=" 1 equals set/a var =%var% "&" 1 Note quotes

4. Set with parameter/p waits for user input
Windows 2000 and Windows are not choice this command, choice waiting for user input function is to be implemented through the set/p
The demo is as follows:
Copy Code code as follows:

@echo off
set/p str= Please enter the characters you want to enter:
echo the string you entered is:%str%
Pause>nul

SET/P Special Applications (Set/p=<nul equivalent to output characters without carriage return, using this property, can make a more beautiful animation), usage examples:
Copy Code code as follows:

@echo off
FOR/L%%i in (1 1) Do (
Set/p=o<nul
FOR/L%%a in (1 1) do Ver>nul
)
Pause>nul

Note: The code uses a for/l%%a in (1 1) does Ver>nul, which is designed to use a for loop to run a command to achieve the delay.
5, using the Set intercept characters to save the following code for Test.bat run view can.
Copy Code code as follows:

@echo Off&color 1f&rem num=15 nums=30
Mode con:cols=80 lines=35
Set "var=1234567890"
Set "num="
Set "nums="
REM First screen Discard
Cls&echo First screen Discard
Echo\&echo%num% var=%var%&echo\
FOR/L%%i in (1 1 3) does call:d a%%i
Echo\
for/l%%i in (0 1 1) do Call:yi%%i
Echo\
for/l%%i in (0 1 2) does call: _yi%%i
The number following the Echo ~ Wave number: A positive sign that discards the variable's front position;
Echo, the number after the comma is positive to indicate the first few after the discard of the variable;
Echo, a negative number after the comma indicates the last few of the discarded variables
Echo\&echo\&pause&color 1e
REM Second screen fetch
Cls&echo Second screen Fetch
Echo\&echo%num% var=%var%&echo\
FOR/L%%i in ( -1-1-3) does call:d a%%i
Echo\
FOR/L%%i in ( -4-1-6) do Call:er%%i
Echo\
Call: _er-8
After the Echo ~ wave is directly followed by a negative number to take the variable
Echo, the number after the comma is positive to indicate the first few after the discard of the variable;
Echo, a negative number after the comma indicates the last few of the discarded variables
Echo\&echo\&pause&color 1f
REM Third screen Replace delete
Cls&echo Third Screen Replace delete
Set "var=abc:\123\ef:g\123\456" Hij "789" klm\ "
Echo\&echo%num%var=%var%
Echo\
Call:san
To the left of the echo = number is the character to be replaced, and to the right is the character to be replaced
The character to be replaced on the left of the echo = number is preceded by A *, which represents the first occurrence of the character and all the preceding characters
Echo\
Echo ========= finished demo ========= Press any key to exit ==========
Pause>nul
Exit
:d an
Set shu=%1
If%shu% LSS 0 (set aa= fetch &set ji= last) Else set aa= discard &set before
If "%shu:~0,1%" = "-" Set shu=%shu:~1%
Set str1=%%var:~%1%%%num%
%ji%%shu% bit%nums% of Set Str2=%aa%var
Call Set str3=%%var:~%1%%%num%
Call Echo%%str1:~0,15%%%%str2:~0,30%%%%str3:~0,15%%
Goto:eof
: Yi
Set shu=%1
If%shu% LSS 0 (set aa= fetch &set ji= last) Else set aa= discard &set before
FOR/L%%i in (1 1 3) Do (
Set str1=%%var:~%1,%%i%%%num%
After the%ji%%shu% bit of set Str2=%aa%var, take%%i bit%nums%
Call Set str3=%%var:~%1,%%i%%%num%
Call Echo%%str1:~0,15%%%%str2:~0,26%%%%str3:~0,15%%
If%%i==3 echo\
)
Goto:eof
: _yi
FOR/L%%i in ( -1-1-3) Do (
Set str1=%%var:~%1,%%i%%%num%
Set str2= discards the first% 1 bits and%%i bits of Var%nums%
Call Set str3=%%var:~%1,%%i%%%num%
Call Echo%%str1:~0,15%%%%str2:~0,28%%%%str3:~0,15%%
If%%i==-3 echo\
)
Goto:eof
: ER
Set sss=%1
Set sss=%sss:~1%
FOR/L%%i in (1 1 3) Do (
Set str1=%%var:~%1,%%i%%%num%
Set str2= the%%i bit from the reciprocal%sss% bit of Var%nums%
Call Set str3=%%var:~%1,%%i%%%num%
Call Echo%%str1:~0,15%%%%str2:~0,24%%%%str3:~0,15%%
If%%i==3 echo\
)
Goto:eof
: _er
Set sss=%1
Set sss=%sss:~1%
FOR/L%%i in ( -1-1-3) Do (
Set str1=%%var:~%1,%%i%%%num%
Set str2= starts at the penultimate%sss% bit of Var and discards the last%%i bit%nums%
Call Set str3=%%var:~%1,%%i%%%num%
Call Echo%%str1:~0,15%%%%str2:~0,20%%%%str3:~0,15%%
If%%i==-3 echo\
)
Goto:eof
: San
Set Str1=%%var: "=%%%num%
Set str2= Delete all ^ "numbers in Var%num%
Set Str3=%var: "=%%nums%
Call Echo%%str1:~0,13%%%%str2:~0,23%%%%str3:~0,30%%
Set str1=%%var:\=%%%num%
Set str2= deletes all \ numbers in Var%num%
Set str3=%var:\=%%nums%
Call Echo%%str1:~0,13%%%%str2:~0,23%%%%str3:~0,30%%
Set str1=%%var::=%%%num%
Set str2= deletes all of the Var:%num%
Set str3=%var::=%%nums%
Call Echo%%str1:~0,13%%%%str2:~0,23%%%%str3:~0,30%%
Echo\
Set Str1=%%var: "= good%%%num%
Set str2= replaces all the ^ "numbers in Var as good characters%num%
Set Str3=%var: "= good%%nums%
Call Echo%%str1:~0,12%%%%str2:~0,20%%%%str3:~0,30%%
Set str1=%%var:\= good%%%num%
Set str2= replaces all \ numbers in Var as a good word%num%
Set str3=%var:\= good%%nums%
Call Echo%%str1:~0,12%%%%str2:~0,20%%%%str3:~0,30%%
Set str1=%%var::= good%%%num%
Set str2= replaces all of the Var's: number as a good word%num%
Set str3=%var::= good%%nums%
Call Echo%%str1:~0,12%%%%str2:~0,20%%%%str3:~0,30%%
Echo\
Set str1=%%var:* "=%%%num%
Set str3=%var:* "=%%nums%
echo deletes the first occurrence of the ^ "number in Var and all previous characters
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo.
Set str1=%%var:*\=%%%num%
Set str3=%var:*\=%%nums%
echo deletes the first occurrence of the number and all the preceding characters in Var
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo.
Set str1=%%var:*:=%%%num%
Set str3=%var:*:=%%nums%
echo deletes the first occurrence of the Var: number and all preceding characters
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo\
Set str1=%%var:* "=ppp%%%num%
Set str3=%var:* "=ppp%%nums%
Echo replaces the first occurrence of the ^ "number in Var with all of the preceding characters as PPP
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo.
Set str1=%%var:*\=ppp%%%num%
Set str3=%var:*\=ppp%%nums%
Echo replaces the first occurrence of the \ number and all preceding characters in Var with PPP
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo.
Set str1=%%var:*:=ppp%%%num%
Set str3=%var:*:=ppp%%nums%
Echo replaces the first occurrence of the: number and all preceding characters in Var with PPP
Call Echo%%str1:~0,15%%%%str3:~0,30%%
Echo\
Goto:eof

Current 1/2 page 12 Next read the full text

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.