Detailed description of set truncation characters in Batch Processing

Source: Internet
Author: User
Tags arabic numbers echo command truncated

Detailed description of set truncation characters
In batch processing, the set function is a bit complicated: Setting variables, displaying the names and values of environment variables, performing arithmetic operations, waiting for user input, string truncation, replacing strings, is one of our Common commands.

In terms of string truncation, it is easy for a newbie to extract the wrong string because he has not noticed the offset. Therefore, this post will describe the usage of the Set truncation character in detail.

Let's take an example:

Set STR = 123456789

Now, I need to extract the first character from the variable 'str'. How can I write the command?
Set Var = % STR :~ 1, 1%? I think this is probably the first reaction of many new beginners who have a rough understanding of the set usage. In fact, this statement extracts the character "2", not the "1" we want"

 

That is, set Var = % STR :~ 1. 1% is extracted from the second character of the string, not the first character. Why?

It turns out that when the set command truncates characters from left to right, it calculates the offset of the characters to be truncated from the starting point of the first character of the entire string. That is to say, when intercepting characters, set calculates the length of the first character of the extracted string that is different from the first character of the entire string. Note that set extracts characters by Offset rather than the absolute position of characters, which is very important. As long as you keep this in mind, when you intercept characters, you will no longer make mistakes on this issue.

Now, we can use a statement to represent the command to intercept characters, that is: Set Var = % STR :~ Offset, length %.

Let's explain in detail the meaning of this statement mode:

First, we need to assign the string to be operated to a variable. In this statement, the string is assigned to the variable STR; then, we need to determine which part of the string we want to extract. For example, we need to extract the string 2nd characters and the next three characters, or extract the string containing 5th characters and the next 4 characters ......, Finally, calculate the offset and length. For example, to extract the string's 2nd characters and the next three characters, that is, extract a string with a Offset of 1 to the first character and a length of 4 to the extracted string. The written statement is set Var = % STR :~ 1, 4%.

So far, we have only talked about simple interception operations. If we encounter complicated extraction requirements, such: extract 2nd characters and all subsequent characters, extract the last three characters, extract the last 2nd and previous three characters, extract the strings except the last four characters ...... What should I do? Don't worry, the set command has fully taken into account our complex requirements during the design. As long as we make slight changes to the character truncation statement we mentioned just now, we can easily complete the task.

We know that the positive and negative numbers can be expressed by the +-symbol. Similarly, the positive and negative directions can also be marked by the +-symbol. When a set is used to intercept characters, the +-symbol is introduced to indicate the direction of the character truncation: The left-to-right truncation is +, and the right-to-left truncation is-. Therefore, set Var = % STR :~ % Can also be written as set Var = % STR :~ + 1, + 4%, but when intercepting from right to left, the situation changes a bit, that is, the starting point of the Offset is calculated based on the last character of the entire string. Now, we can answer some of the questions raised in the previous section:

Extract the last three characters: Set Var = % STR :~ -3%
Extract the last 2nd and the previous three characters: Set Var = % STR :~ -6, 4%
Extract the string except the last four characters: Set Var = % STR :~ 0,-4%.

After reading the code for the above three requirements, you may have a new question: how can there be only one number in the first article? Article 3 What does the last digit mean by a negative number?

Originally, in set Var = % STR :~ In a statement such as offset and length %, if there is no comma or a later length, all characters at and after the offset are truncated. If the length value is negative, the last few characters are discarded.

Now we can extract the characters at any position (assuming set STR = 123456789 ):

① Extract 1: Set Var = % STR :~ 0, 1% or set Var = % STR :~ 0,-8% or set Var = % STR :~ -9,1%
② Extract 2: Set Var = % STR :~ 1,1% or set Var = % STR :~ 1,-8% or set Var = % STR :~ -8, 1%
③ Extract 9: Set Var = % STR :~ 8, 1% or set Var = % STR :~ 8% or set Var = % STR :~ -1,1% or set Var = % STR :~ -1%
④ Extract 123: Set Var = % STR :~ 0, 3% or set Var = % STR :~ 0,-6% or set Var = % STR :~ -9,3%
5. Extract 234: Set Var = % STR :~ 1,3% or set Var = % STR :~ 1,-6% or set Var = % STR :~ -8, 3%
⑥ Extract 789: Set Var = % STR :~ 6,3% or set Var = % STR :~ 6% or set Var = % STR :~ -3,3% or set Var = % STR :~ -3%

Finally, let's summarize the character truncation rules:

1. You can use set Var = % STR: ~ to intercept a string :~ The value 1 and the value 2% are implemented;

2. The Truncation of characters is calculated based on the offset, rather than the absolute position of the characters;

3. When the value 1 is a positive number, it indicates to intercept from left to right. When the value 1 is a negative number, it indicates to intercept from right to left;

4. When the value 2 is a positive number, it indicates the length of the string to be truncated; when it is a negative number, it indicates the length of the last few characters to be discarded;

5. If the value 2 and its previous comma do not exist, it indicates that the truncation is the second (value 1 + 1) character and all subsequent characters;

You will surely get the following results if you repeat the next batch of exercises:
Example: A character truncation exercise tool

@ Echo off & Color A & mode con Cols = 90 lines = 12 Title truncation character exercise tools by bat-zw19750516set Var = 1234567890 1234 five sixty-nine zero CLS & Echo. & Echo original pattern: % var % echo. & Echo note that there are 10 Arabic numbers followed by 10 Chinese numbers. Remember to take a look after the interception. Echo. & Echo Command Format: % var :~ M, N %, where M is the first parameter, which can be positive or zero, and N is the second parameter, which can be negative or not zero. Echo. & Echo is not set to exit to facilitate repeated exercises. to exit, close the window directly. Echo. & Echo press any key to start the exercise. & Pause> NUL: beginset A = & Set B = & set "c =" CLS & Echo. & set/p a = enter the first parameter (an integer smaller than 20): Echo %: -= % | findstr "[^ 0-9]" & goto wrongif "% A %" equ "Goto wrongif % A % geq 20 goto wrongcls & Echo. & set/p B = enter the second parameter (an integer greater than-20 and not zero): Echo % B: -= % | findstr "[^ 0-9]" & goto wrongif "% B %" equ "" Goto wrongif % B % equ 0 goto wrongif % B % LSS-20 goto wrongset/A c = 20-a BIF % A % LSS 0 if % B % LSS 0 if % A % geq % B % goto wrongif % A % G Tr 0 if % B % LSS 0 if % C % Leq 0 goto wrongcls & Echo. & Echo original pattern: % var % echo. & echo the command you entered is: % var :~ % A %, % B % echo. & call, the echo captured pattern is: % var :~ % A %, % B % echo. & Echo press any key to practice again. & Pause> NUL & goto begin: wrongcls & Echo. & Echo input is not required. Please enter it correctly. & Ping/N 2 127.1> NUL & goto begin

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.