Common practical skills for Batch Processing

Source: Internet
Author: User

Compile Batch Processing Code For more than a year, I have accumulated some practical code writing skills for batch processing. Now I am sharing them with you. If you can make good use of the code, it will greatly simplify the coding work and improve the code execution efficiency to a certain extent.
1. Various prompts during code execution are not displayed

Many times, in order to prevent irrelevant information on the screen, we need to block some prompts generated during command execution, sometimes blocking error information, sometimes it is to block the information of successful command execution, and sometimes both need to be blocked, then we can redirect various prompts to the empty device, the specific code is:
① To shield the information about successful command execution, add code> NUL at the end of the statement. For example, copy a.txt D: \ test> NUL
② To shield error information, add Code 2> NUL at the end of the statement. Note that there must be spaces before the number 2. For example, MD Test 2> NUL. If the folder test already exists in the current directory, no error is prompted when you execute this statement;
③ If you want to block the prompt and error information generated after correct code execution, add code> NUL 2> NUL at the end of the statement. For example, the statement copy a.txt D: \ test> NUL 2> NUL does not display any prompts on the screen whether or not the command is successfully executed;
Using NUL 2> NUL sentence pattern can easily achieve many purposes. For example, when creating a folder, you do not need to use the if statement to check whether a directory already exists, direct MD Test 2> NUL can improve code execution efficiency.

2. CD.> test.txt

When many people encounter such a statement for the first time, they will be puzzled: Isn't CD a command to jump to a directory? How can I create a 0-byte file? For a while, I used echo.> test.txt is used to create an empty file. However, the size of the empty file is not 0 bytes, and the task cannot be completed well in harsh conditions. In fact, theoretically, there are an infinite number of statements for creating 0-byte files. As long as you understand the mysteries of these statements, you can construct them as you like, just CD.> test.txt is the most concise method, which is consistent with my "simplified solution. The secret to constructing a 0-byte file is that as long as no content is redirected to the file, the 0-byte file will be generated as the command is executed. For example, Dir 2> test.txt, ARP> text.txt, ABC 4> test.txt ......, You only need to conform to the format of "arbitrary content + space + 2 + numbers less than 10 +> test.txt. Please note that we should never be blinded by various error messages on the screen. Our ultimate goal is to generate 0-byte files. As for error messages, we can use 2> NUL to block them. For more detailed discussion, please refer to my post in CN-DOS: several commands that can be used to construct a 0-byte text file.

3. DEL/A/F *. txt

We know that the single del command cannot delete files with hidden or read-only attributes. Therefore, many people naturally want to use the attrib command to remove the attributes of the files to be deleted first. In fact, the operation process does not need to be so complicated. You only need to add the/A/F parameter to Del.

4. Echo. Echo; ECHO/and echo \

In many people's minds, only echo is used to generate line breaks. one. In fact, someone has found the four methods listed in the title so far. If you are interested, you may wish to use them again. You can also show off them in front of cainiao.

5. Echo. % STR % and echo % STR %

Echo. Hello! And echo hello! What is the difference? In terms of the output of specific strings, they are completely equivalent in effect; however, when it is hello! After the variable is replaced, the results will be completely different under specific circumstances! Try to leave the variable STR empty, and then execute the two statements in the batch file to see their prompts. Is the result returned? To sum up, when the value of STR is null, Echo. % STR % outputs an empty row, while echo % STR % returns the current echo status. When the for statement is used to read the text with blank lines and output the text with the echo statement, the echo. % STR % statement is undoubtedly the top choice. In addition, the 4th skills are also applicable here.

6. concatenate multiple strings into one line

If you want to splice multiple strings in a text into one line, set STR = % I in the for statement, and then, the next step is set STR = % I! Str! To splice the last sentence with the previous one. In fact, only one set/p str = % I <NUL can be used in these two steps. The simple DEMO code is as follows:Copy codeThe Code is as follows: @ echo off
For/F "delims =" % I in (test.txt) do set/p str = % I <NUL
Echo.
Pause> NUL

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.