Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used practical skills for batch processing

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what the commonly used batch processing skills are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Do not display all kinds of prompts during code execution

In many cases, in order to prevent irrelevant information from appearing on the screen, we need to block some prompts generated during command execution, sometimes error messages, sometimes information about successful execution of commands, and sometimes both need to be shielded, so we can redirect various prompts to the empty device. The specific code is as follows:

① if you want to mask the information that the command was executed successfully, add code > nul at the end of the statement. For example, copy a.txt d:\ test > nul

② if you want to mask error messages, add code 2 > nul at the end of the statement. Note that the number 2 must be preceded by a space. For example, md test 2 > nul, if the folder test already exists in the current directory, then executing this statement will not give an error prompt

③ if you want to mask both prompts and error messages after the correct execution of the code, add code > nul 2 > nul at the end of the statement. For example, the statement copy a.txt d:\ test > nul 2 > nul will not show any prompts on the screen regardless of whether the command is executed successfully or not.

Make good use of nul 2 > nul sentence, you can easily achieve many purposes, for example, when creating a folder, you do not need to use if statements to check whether a directory already exists, you can directly md test 2 > nul, which can improve the efficiency of code execution.

2. Cd. > test.txt

Many people are puzzled when they come across such a statement for the first time: isn't cd a command to jump to a directory? How can there be the function of creating 0-byte files? There was a time when I used echo. > test.txt to create an empty file, but although the empty file does not have any content, but the size is not 0 bytes, in demanding cases, can not complete the task very well. In fact, in theory, there are infinitely many statements to create a 0-byte file, and as long as you understand the secret of it, you can construct it at will, but cd. > test.txt is the most concise method, in line with my consistent proposition of "problem-solving simplicity". The secret of constructing a 0-byte file is that as long as nothing is redirected to the file, the 0-byte file will be generated with the execution of the command. For example, dir 2 > test.txt, arp > text.txt, abc 4 > test.txt. As long as it conforms to the format of "any content + spaces + numbers below 2 and below 10 + > test.txt". It should be noted that do not be blinded by all kinds of error messages on the screen, our ultimate goal is to generate 0-byte files, as for error prompts can be completely shielded with 2 > nul. For a more detailed discussion, please refer to my post on CN-DOS: several commands that can construct a 0-byte text file.

3. Del / a / f * .txt

We know that the del command alone cannot delete files with hidden or read-only attributes, so many people naturally think of using the attrib command to remove various attributes from the files to be deleted first. In fact, the operation process does not have to be so tedious, just add the parameter / a / f to del.

4 、 echo. Echo; echo/ and echo\

In the minds of many people, the only way to generate newline characters is echo. One, in fact, so far, someone has found out the four methods listed in the title. If you are interested, you might as well use it instead, or you can show off in front of a rookie.

5. Echo.%str% and echo% str%

Yes, echo.hello! And echo hello! What's the difference? In terms of the output of specific strings, they are exactly the same in effect; however, when hello! After replacing with variables, the results will be very different under certain circumstances! Try leaving the variable str empty, and then execute each of these two statements in the batch file to see their hints. Did you get the results? To sum up: when the value of the variable str is empty, echo.%str% prints a blank line, while echo% str% returns the current state of echo. When reading text with blank lines with a for statement and outputting it with an echo statement, the echo.%str% statement is undoubtedly the best choice. In addition, the techniques in Article 4 also apply here.

6. Stitching multiple lines of strings into one line

If you want to concatenate multiple lines of strings in a text into one line, according to the general idea, in the for statement, first set str=%%i, and then, the next step is set stringing%% iSystr!, so as to achieve the concatenation of the latter sentence with the previous sentence. In fact, these two steps use only one set / p str=%%i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report