Detailed description of the batch processing Set command helps you understand page 1/2 of the set command

Source: Internet
Author: User

Set and E are translated as "settings", which is equivalent to "orders" in mathematics ".
For example, set X = 5 means X = 5.
Common use example of set:
1. set display environment variables:
For example, enter:
Set displays all current system environment variables.
Set c displays all variables starting with c or C (that is, they are case insensitive );
Set com will display all variables starting with com or COM (Case Insensitive );
2. set environment variables. Usage example: set expression,
For example, set str = OK
Is to set the value of the variable str to the string "OK"
Note: set str = OK>
This error will occur during batch processing and the system will prompt: "The command syntax is incorrect ."
The reason is that, because the character ">" with the value "OK>" in the str variable is a special character, it will be processed by the system as a targeting character, so an error occurs during running, what if you want to set the str variable with the value "OK>? Solution 2: Escape special characters and add a "^" before the special character, such as: set str = OK ^> (this method is rarely used, because there is not much application value)
A common solution is to enclose the entire expression in quotation marks after set, such as: set "str =>"
3. set Value Calculation with parameter/
The/A parameter of set enables SET to support mathematical operations such as addition and subtraction of mathematical symbols! Supported mathematical operations include (descending priority ):
()-Group
! ~ --Unary operator
*/%-Arithmetic Operator
+--Arithmetic Operator
<>-Logical shift
&-Bitwise "and"
^-Bitwise "exception"
|-Bitwise "or"
= * =/= % = + =--Value assignment
<=>>=
,-Expression Separator
Example: set/a num = 2*10, so that the value of the variable num will change to: 20 Copy codeThe Code is as follows: set/a var = 2-1. What is the result? If you cannot see the result, echo % var % .....
Set/a var = 2*2 Multiplication
Set/a var = 2/2 division operation
Set/a var = (1 + 1) + (1 + 1) the result is 4. You can understand it!
Set/a = 1 + 1, B = 2 + 1, c = 3 + 1 after running, a 4 is displayed, but we use
Echo % a % B % c % and check the result. Other mathematical operations are also effective !, This is the number of "Bucket ".
Role!

Sometimes we need to directly add or subtract the original variable to use this syntax.
Set/a var + = 1. The original syntax is set/a var = % var % + 1.
The results are the same.
For "logical or remainder operators", we need to enclose them with double quotation marks. Let's look at the example.Copy codeThe Code is as follows: set/a var = 1 "&" 1 and the result is displayed. Other logic or remainder operators are used.
Set/a var = 1 "+" 1 Operation
Set/a var = 1 "%" 1 modulo operation
Set/a var = 2 "<"
Set/a var = 4 ">" 2. I don't remember the name in mathematics ....

These symbols can also be used in simple usage such
Set/a var "& =" 1 equals set/a var = % var % "&" 1 Note quotation marks

4. set with parameter/p is waiting for user input
In windows 2000 and windows, there is no choice command. The choice waiting for user input is implemented through set/p.
The demo is as follows:Copy codeThe Code is as follows: @ echo off
Set/p str = enter the characters you want to enter:
Echo the string you entered is: % str %
Pause> nul

Special Application of set/p (set/p = <nul is equivalent to output characters without carriage return. With this feature, you can make beautiful animations). Usage example:Copy codeThe Code is as follows: @ echo off
For/l % I in (1 1 70) do (
Set/p = O <nul
For/l % a in (1 1 50) do ver> nul
)
Pause> nul

Note: The Code uses a for/l % a in (1 1 50) do ver> nul, it uses a for loop to run a command to achieve latency.
5. Use the set truncation character to save the following code as test. bat and run it.
Copy codeThe Code is as follows: @ echo off & color 1f & rem num = 15 nums = 30
Mode con: cols = 80 lines = 35
Set & quot; var = 1234567890 & quot"
Set "num ="
Set "nums ="
Rem first screen discard
Discard the first screen of cls & echo
Echo \ & echo % num % var = % var % & echo \
For/l % I in (1 1 3) do call: dan % I
Echo \
For/l % I in (0 1) do call: yi % I
Echo \
For/l % I in (0 1 2) do call: _ yi % I
Echo ~ Number after the Tilde: positive number indicates that the prefix of the variable is discarded;
Echo. the number after the comma is positive, indicating the first few digits after the removal of the variable;
Echo. If the comma is followed by a negative number, the last few digits of the variable are discarded.
Echo \ & pause & color 1e
Rem second screen capture
Capture the second screen of cls & echo
Echo \ & echo % num % var = % var % & echo \
For/l % I in (-1-1-3) do call: dan % I
Echo \
For/l % I in (-4-1-6) do call: er % I
Echo \
Call: _ er-8
Echo ~ The Tilde is followed by a negative number to indicate the last few digits of the variable.
Echo. the number after the comma is positive, indicating the first few digits after the removal of the variable;
Echo. If the comma is followed by a negative number, the last few digits of the variable are discarded.
Echo \ & pause & color 1f
Rem third screen replacement and Deletion
Replace and delete the third screen of cls & echo
Set "var = abc: \ 123 \ ef: g \ 123 \ 456" hij "789" klm \"
Echo \ & echo % num % var = % var %
Echo \
Call: san
Echo = character to be replaced on the left and character to be replaced on the right
Echo = + A * sign before the character to be replaced on the left, which indicates the first occurrence of the character and all the characters before it
Echo \
Echo ========= demo completed =========== press any key to exit =========
Pause> nul
Exit
: Dan
Set shu = % 1
If % shu % lss 0 (set aa = get & set ji = final) else set aa = discard & set ji = before
If "% shu :~ 0, 1% "="-"set shu = % shu :~ 1%
Set str1 = % var :~ % 1% % num %
Set str2 = % aa % var % ji % shu % bit % nums %
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 = get & set ji = final) else set aa = discard & set ji = before
For/l % I in (1 1 3) do (
Set str1 = % var :~ % 1, % I % num %
Set str2 = % aa % var % ji % shu % bit and get % I % 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 = discard the first % 1 bit and % I bit % nums % of var
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 = get % I bit % nums % from the last % sss % bits of var
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 = start from the last % sss % bits of var and discard 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 ^ "signs in var % num %
Set str3 = % var: "= % nums %
Call echo % str1 :~ 0,13% % str2 :~ 0,23% % str3 :~ 0, 30% %
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 = delete all in 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 = replace all ^ "IN var WITH" % num %"
Set str3 = % var: "= Good % nums %
Call echo % str1 :~ 0, 12% % str2 :~ 0, 20% % str3 :~ 0, 30% %
Set str1 = % var: \ = Good % num %
Set str2 = replace all \ numbers in var with 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 = replace all values in var: % 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 the characters above it.
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 in var and all the characters above it.
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 in var and all the characters before it.
Call echo % str1 :~ 0, 15% % str3 :~ 0, 30% %
Echo \
Set str1 = % var: * "= ppp % num %
Set str3 = % var: * "= ppp % nums %
Echo replaces the ^ "that appears for the first time in var and all the characters above it with ppp
Call echo % str1 :~ 0, 15% % str3 :~ 0, 30% %
Echo.
Set str1 = % var: * \ = ppp % num %
Set str3 = % var: * \ = ppp % nums %
Echo replaces the \ sign that appears for the first time in var and all the characters above it 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 in var: The number and all the characters above it are ppp
Call echo % str1 :~ 0, 15% % str3 :~ 0, 30% %
Echo \
Goto: eof

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.