Learn batch processing, it's easy to learn with your heart! Another good opinion on page 1/3

Source: Internet
Author: User
Tags echo command echo display

This is a technical tutorial. I sincerely express my meaning in simple words. As long as you can read and understand the content, you can learn the knowledge. The purpose of this tutorial is to let every friend who has read these texts remember one sentence: If love can make things easier, make it easier! The method for reading this tutorial is slow! Slowly, just like a woman and a cup of tea, you will find that many things that were just in front of you suddenly become far away, some very distant things have suddenly returned to our eyes ..
First, let's outline what batch processing is. So far, I have not been able to give a suitable definition of batch processing-Not many experts have given it-I don't know-I'm not necessarily convinced after reading it-I'm a cainiao, of course, I don't need to mention it. But I want to sum up a "more appropriate", and I believe I can explain it clearly, let more cainiao know what this is and what you can do with it. Maybe you will "Fall In Love With Batch Processing unconditionally" because of this article, so my goal is achieved-I am trying to make you fall in love with it, so I am so jealous, what can you do ?? Really, sometimes love is so arrogant, that's no reason, it's so shameless! Really!
According to my understanding, the essence of batch processing is a collection of DOS commands in a certain order.
OK, never claver and get to business ). BATCH Processing, also known as a BATCH processing script, is translated into BATCH in English. The BATCH processing file suffix BAT takes the first three letters. It does not have a fixed format, as long as the following line is followed, it is OK: each line can be viewed as a command, each command can contain multiple sub-commands, starting from the first line of execution, until the last line ends, it runs on DOS. Batch Processing has a distinctive feature: convenient, flexible, powerful, and highly automated. I don't want to make my own tutorials boring, because code is involved (the batch processing content is code ?) The problem is boring, and few people can calm down with full screen code. So I will use a lot of simple and practical examples to let my friends who have read this tutorial experience the charm of batch processing, feel the strange character of batch processing, and unconsciously love batch processing (dizzy, how is love? What is the relationship between batch processing and love? Answer: No !). Let's say "Gossip": to learn batch processing well, the DOS basics must be solid! Of course, flexible mind is also an important aspect.
Example 1: first, give the most easy batch processing script to familiarize everyone with its face and save the following commands as name. bat and then execute (only the code is provided in the future, and the Save method is similar to the execution method ):
Ping sz.tencent.com> a.txt
Ping sz1.tencent.com> a.txt
Ping sz2.tencent.com> a.txt
Ping sz3.tencent.com> a.txt
Ping sz4.tencent.com> a.txt
Ping sz5.tencent.com> a.txt
Ping sz6.tencent.com> a.txt
Ping sz7.tencent.com> a.txt
Exit
Can you understand it all? Is it easy? However, after performing this batch, you can create a file named a.txt on your own. The recorded information can help you quickly find the fastest QQ server and stay away from the painful process of transferring from the server. Here> means to put the things obtained by the preceding command in the following place,> and> the growth file (think of how to destroy it ??). By the way, this batch processing can also be combined with other commands to achieve a completely automated judgment of the server speed. After the execution, the fastest Server IP address is directly displayed, isn't it great? It will be detailed later.
Example 2: an outdated example (a. bat) is provided ):
@ Echo off
If exist C: \ Progra ~ 1 \ Tencent \ AD \ *. gif del C: \ Progra ~ 1 \ Tencent \ AD \ *. gif
A. bat
Why is this an outdated example? Very simple, because almost no one has used QQ with advertisements (KAO, My QQ also shows friends !!), So it is almost useless. However, once it was used, it could not be a small glimpse: deleting QQ ads and making the dialog box clean. The address used here is the default installation address of QQ. The default batch processing file name is a. bat. You can modify it as needed. The if command is used in this script to determine and delete ad images in a timely manner. You only need to disable the DOS window after the command is executed, without pressing CTRL + C to forcibly terminate the command, it keeps monitoring whether there are advertisement images (QQ also keeps checking whether its advertisement has been deleted ). Of course, this script occupies a little bit of memory.
Example 3: Use the batch processing script to check whether the glacier is in progress. The script content is as follows:
@ Echo off
Netstat-a-n> a.txt
Type a.txt | find "7626" & echo "Congratulations! You have infected GLACIER! "
Del a.txt
Pause & exit
Here, the netstat command is used to check the status of all network ports. You only need to know the ports used by common trojans to easily determine whether they have been attacked by the glaciers. However, this is not certain, because the default port 7626 of the glacier can be modified completely. This section only describes the methods and ideas. Here we will introduce a slight change to the method and idea to check other Trojan scripts. Then we will change it and add the parameter and port and information list files, the script automatically detects all Trojans. Haha, isn't it fun? The combined commands & and pipeline commands are also used in the script, which will be detailed later.
Example 4: Use batch processing to automatically clear system spam. The script is as follows:
@ Echo off
If exist c: \ windows \ temp \ *. * del c: \ windows \ temp \*.*
If exist c: \ windows \ Tempor ~ 1 \ *. * del c: \ windows \ Tempor ~ 1 \*.*
If exist c: \ windows \ History \ *. * del c: \ windows \ History \*.*
If exist c: \ windows \ recent \ *. * del c: \ windows \ recent \*.*
Save the above script content to autoexec. bat, and automatically delete the system garbage every time the system is started. Pay attention to two points: 1. DOS does not support long file names, so Tempor ~ appears ~ 1. You can make changes based on your actual situation so that they meet your requirements.
How are you interested in the batch processing script? Did you find yourself falling in love with this stuff? Don't be happy too early. Love is not a simple task. It may bring you happiness and happiness. Of course, it can also make you think about jumping out of the building. If YOU know it's hard to continue, I 've got YOU! Continue to work hard. Maybe you don't necessarily get true love at the end of the day (it's possible that people who love it know it), but you can feel the whole process of love. It's sour, bitter, and spicy. Do you know if it's sweet.
Why is batch processing related to love? I am not bored, nor is it because it is interesting to write in this way. There are two reasons: first, there are many similarities between batch processing and love, in some cases, I can't explain it clearly with a "professional" line (I don't doubt my ability to express myself, but it's hard to make things clear, but what is love? How do I know !!), Maybe you will be able to get twice the result with half the effort. Why not? Second, I am in a bad status during this period. I have a cold, a fever, a headache, and a nose, but I am still suffering from emotional mental damages. I am tired of people. I am sorry to say a few words when I write a tutorial, everyone needs to buy a dog skin plaster and ignore it (maybe it's a bit effective-it won't let you look at it and then fall asleep, so you can bid your head and come to me to reimburse your medical expenses ). Maybe in the next tutorial, you will see the heroes of Yang Guo, Zhang Wuji, and other elders.
My friends who have read chapter 1 must have a preliminary impression on batch processing and know what it is. But do you know where the essence of batch processing is? In fact, it is very simple: the idea should be flexible! Nothing can be done, just unexpected. This is a little different from love, because the world of love is the world of two people, wishful thinking is not called love (Supplement: it is called Single Love. Nonsense !) Batch processing is a paradise for one person. You can do whatever you want without reaching the realm!
Batch Processing looks disorganized, but it is logically not inferior to other programming languages (such as assembly). If the script you write is messy, although each line of commands is correct, however, after executing the command from the beginning to the end, you may not necessarily get the expected result, maybe it is a screen of Bad command or fail name. This has something in common with love: operating by steps, and missing or increasing steps may lead to unexpected results. A friend who falls in love, I believe this sentence is not certain. My love batch processing, the output result is not Bad command or fail name, the screen is shown as follows: 'Your love' is not an internal or external command, it is not a running program or batch processing file. Then the cursor keeps flashing and waits for the wrong input.
Starting from this chapter, we will introduce the commonly used commands in batch processing in a simple way. Many common DOS commands are widely used in batch processing scripts, which are the BODY part of the batch processing script, however, batch processing is more flexible and more automated than DOS. To learn batch processing well, DOS must have a solid foundation. Here we will only describe some doscommands that are rarely used (relatively speaking). Common commands such as COPY and DIR will not be introduced (these seemingly simple commands are actually very complicated, I am afraid I cannot make it clear !).
Example 5: first look at an instance. This is a very interesting script. It is a small and practical tool that shows the batch processing features in full automation. First, let's introduce the history of this script: we all know the process of installing the assembler (MASM). First, we need to compile and connect the source code, and then execute it, there are many links in the middle that require a lot of input, which is very troublesome (only experienced friends can understand ). How can this process be simplified? During our compilation course design, I was forced to write this script, which was very helpful. Check the script content:
@ Echo off
: Close echo
Cls
: Clean screen
Echo This programme is to make the MASM programme automate
: Display info
Echo Edit by CODERED
: Display info
Echo Mailto me: qqkiller *** @ sina.com
: Display info
If "% 1" = "" goto usage
: If input without paramater goto usage
If "% 1" = "/? "Goto usage
: If paramater is "/? "Goto usage
If "% 1" = "help" goto usage
: If paramater is "help" goto usage
Pause
: Pause to see usage
Masm % 1.asm
: Assemble the. asm code
If errorlevel 1 pause & edit % 1.asm
: If error pause to see error msg and edit the code
Link % 1.obj & % 1
: Else link the. obj file and execute the. exe file
: Usage
: Set usage
Echo Usage: This BAT file name [asm file name]
Echo Default BAT file name is START. BAT
: Display usage
Don't be scared by this pile of things first, so calm down and take a closer look (think about how the first section in Chapter 1 was written !!). The explanation of each line of commands has been given. The content after the two colons is the E text explained in the previous line. (If you are afraid of E text, you don't have to worry about it. It's easy, I understand it at a Glance. I really don't understand it. I don't know how to look up dictionaries. Is it so lazy ?), It is not displayed during script execution and does not play any role. There is a colon at the beginning of the last 5th rows. It's not a mistake! The specific functions will be detailed later. In this script, masm and link are Assembler and connection programs. They must be related to the edit Program and the source code you want to edit. (Of course, this script is also nonsense !) Together in the current directory. Using this batch processing script can minimize manual input. during the entire process, you only need to press a few enter keys to implement automatic conversion from the Assembly source code to the executable exe file, intelligent judgment: if an error occurs in the source code during compilation (Assembly fails), the error message is automatically paused and automatically enters the source code editing interface after pressing any key; if the source code compilation is successful, connect and automatically execute the generated exe file after the connection. In addition, because of the simplicity and flexibility of the batch processing command, this script also has a good option to modify, simple modification can meet the machine habits of different friends. If you are learning compilation, do not forget to take an internship!
In this script, the following commands are displayed: @, echo,:, pause,: And goto, %, and if. This chapter describes these commands.
1 ,@
There are no strangers to this symbol. How can it be used for batch processing? Well, it's not its fault. batch processing is inseparable from it, or it's not perfect. It does not display the command line after it in the execution window (what a detour !). Well, in other words, the command line will not be displayed if the line has its first line. In Example 5, in the @ echo off line at the beginning, @ is used to prevent the script from displaying the echo off part at execution. Do you understand this? Still not quite familiar with it? It doesn't matter. After reading the echo command introduction, you will naturally understand it.
2. echo
The Chinese characters are "feedback" and "Echo. It is actually a switch command, that is, it has only two States: open and close. So there are two commands, echo on and echo off. Directly executing the echo command will display the status (off or on) of the current echo command. Executing the echo off command will turn off echo display, and all subsequent commands will not display the command itself, only the execution result is displayed, unless the echo on command is executed. In Example 5, the @ command in the first line is combined with the echo off command to achieve two purposes: the echo off command itself is not displayed, and the commands in the subsequent lines are not displayed. It is a bit messy, but if you practice it, you will get a 3-minute package and will not refund the money!
Another way to use the echo command is to use it to display information! For example, in Row 5, the Default BAT file name is START. BAT is displayed in the window after the script is executed, but the echo command itself is not shown (Why ??).
Another method of echo command 2: You can directly edit a text file. Example 6:
Echo nbtstat-A 192.168.0.1> a. bat
Echo nbtstat-A 192.168.0.2> a. bat
Echo nbtstat-A 192.168.0.3> a. bat
The preceding script content can be edited by entering the content in the command line with a carriage return on each line. Finally, a file a. bat will be generated in the current directory and the result will be obtained after direct execution.
3 ,::
The function of this command is very simple. It is a comment command, which is equivalent to the rem command in the batch processing script. The content after it is not displayed during execution, and it does not play any role, because it is only a comment, but increases the readability of the script, and the/* in the C language /*............ */Similar. Everyone on Earth can understand it, so I won't say much.
4. pause
Pause (look at your workman). I always think it is the simplest command in batch processing, simple and practical. It is used to pause the current program process and display a line of information: press any key to continue .... In Example 5, this command is used twice. The first function is to let the user see the program information, and the second is to display the wrong assembly code information (not the information it wants to display, instead, the masm program is temporarily suspended when an error message is displayed, so that you can see where your source code is wrong ).
5.: And goto
Why should we introduce these two commands together? Because they are inseparable, no matter which one is missing or which one is missing, errors will occur. Goto is a jump command, which is a tag. When the program runs to goto, it will automatically jump to: the defined part is executed (is it inseparable ?). In Example 5, the first row of the last 5th rows appears: when the program runs to goto, it automatically jumps to the execution of part of the label definition. The result is the script usage (usage is the label name ). It is not hard to see that the goto command is based on the colon and Tag name to find the place where it should jump. They are a one-to-one correspondence. The goto command is often used in combination with the if command. For the specific usage of the two commands, see Example 5.
Another way to use the goto command is to terminate the program in advance. In the program, use the goto command to jump to a tag. The content of this tag is defined as exit. For example:
......
Goto end
......
: End
Here: end is in the last line of the script! In fact, this example is very mentally retarded. You will know about the if Command and the combined command later.
6.%
This percent number is strictly not a command. It is only a parameter in batch processing (except when multiple % are used together, which will be detailed later ), but never underestimate it as a parameter. (See how many places in Example 5 use it ?), The batch processing function is reduced by 51%. Let's take a look at Example 7:
Net use \ % 1 \ ipc $ % 3/u: "% 2"
Copy 11.BAT \ % 1 \ admin $ \ system32/y
Copy 13.BAT \ % 1 \ admin $ \ system32/y
Copy ipc2.BAT \ % 1 \ admin $ \ system32/y
Copy NWZI. EXE \ % 1 \ admin $ \ system32/y
Attrib \ % 1 \ admin $ \ system32.bat-r-h-s
The above code is part of the Bat. Worm. Muma virus. % 1 represents the IP address, 2% represents the username, and 3% represents the password. The execution format is: script file name parameter 1 parameter 2 ....... Assume that the script is saved as a. bat. The execution format is as follows: a IP username password. Here, IP, username, and password are three parameters, which are indispensable (because the program cannot run correctly, not because the parameter syntax is missing) during script execution, the script automatically uses your three parameters in sequence (Remember, in turn! Is also a one-to-one correspondence .) Replace 1%, 2%, and 3% for the purpose of flexible use (imagine if ip, username, and password are defined in the script, the role of the script is fixed, but if % is used, different parameters can achieve different purposes. Is it more flexible ?).
The usage of this parameter will be described in subsequent sections. You must be very skilled. This requires a lot of exercises and effort!
This chapter is written here. Some may have asked: Why didn't I introduce the if command? Well, it's not something I forgot, but it's not easy to make it clear. I'll talk about it in the next chapter! If you are a beginner, I am afraid it is enough to digest this chapter. Remember one sentence: DOS is the BODY of batch processing. Any DOS command can be used in batch processing scripts to complete specific functions. So far, have you ever thought of writing something with automation in your own stomach? It is very simple. It is just a collection of doscommands. I believe that, if you call yourself a genius, you will have automatically completed the DOS part of the Computer Level examination questions using batch processing.
Annoying! It's like a half-old woman has reached the menopause. She wants to say a few things and feels uncomfortable about everything. Knowing that there are tigers in the mountains, they prefer to go through the mountains and finally leave behind a scar without success, only to find that they are so fragile, so small, so vulnerable. On the verge of collapse, I suddenly recalled my last flat moments. I really miss it. (In fact, I really don't like flat people, but I don't like flat people ). I need to vent. I beat the keyboard with my fingers, and these words appeared on the screen in a burst of rhythmic sounds. But is this another way of venting? The Chinese are still amazing. As early as thousands of years ago, Kong's husband and wife said, "only women and people are difficult to raise". I really appreciate it! Although it is venting, please rest assured that with my temper, since I decided to write this tutorial, I will try my best to write it well and write it perfectly without leaving any regrets for myself, otherwise, this tutorial is not written by me!
There was a classic batch processing tutorial that appeared on your screen. You didn't save it until you couldn't find the link to it. The greatest pain in the world is this. If God could give you another chance, you would say three words to the Tutorial: I love you! If you have to add a deadline for this love, you hope it will be 100. Because after March 100, you may have crashed! Now, this batch processing tutorial you are reading appears on your screen. Although it is not as good as the classic one you have read, if you barely continue. Will you love it? Will the time be 50 years long? The answer is: Try it.
Several of the most important commands in the batch processing script will be described in detail in this chapter, but unfortunately, I have not mastered some details yet, even some points. Just as you do not know much about love. But I have been working hard, even if I have never been rewarded. So it may be more general, but I will tell you the method. The rest is time, and you need to hone it on your own. Let's work together. Freezing is not a day's cold, but a day's work. Some things, such as batch processing, such as loving a person, cannot be achieved quickly, or even suffer from hardships and little gains. I would like to reiterate that when reading this tutorial, you must calm down, unless you have mastered all the things in this tutorial-but you don't have to read it, it's a waste of time!
7. if
Next, let's talk about the if command. In general, the if command is a command that indicates judgment. According to each result, it can correspond to a corresponding operation. The three usage methods are described separately here.
(1) Input judgment. Let's look at the words in case 5:
If "% 1" = "" goto usage
If "% 1" = "/? "Goto usage
If "% 1" = "help" goto usage
Here, the input parameter is determined. If the parameter is null (No parameter), the system jumps to usage. If the parameter is /? Or help (if you look at the help of a command, do you enter /? Or help. Here we do this only to make the script look more like a real Program) and jump to usage. The negative form can also be used to indicate "not equal to", for example, if not "% 1" = "" goto usage, it indicates that if the input parameter is not null, it will jump to the usage. (In practice, this is meaningless. Here we will introduce the usage, so it won't be enough .) Is it easy? In fact, the style will be understand after translation.
(2) Existence judgment. Let's look at this sentence in Example 2:
If exist C: \ Progra ~ 1 \ Tencent \ AD \ *. gif del C: \ Progra ~ 1 \ Tencent \ AD \ *. gif
Delete gif files if they exist. Of course, Example 4 is the same. Note: The condition judgment here is to judge whether there is a existence. Of course, you can also judge whether there is a nonexistent. For example, the following sentence "exit the script if there are no gif files": if not exist C: \ Progra ~ 1 \ Tencent \ AD \ *. gif exit. Only one more not indicates no.
(3) determine the result. Let's take the example. (I didn't expect my own script to be so useful, huh, huh ):
Masm % 1.asm
If errorlevel 1 pause & edit % 1.asm
Link % 1.obj
Compile the source code first. If the source code fails, the error message will be paused. Press any key to automatically enter the editing page. Otherwise, the obj file generated by the link program will be connected. Here we will only describe what is related to the if command. & the command will be discussed later. This method is to first determine the return code after the previous command is executed (also known as the error code, the DOS program has a return code after running ), if it is consistent with the defined error code (the defined error code here is 1), perform the corresponding operation (here the corresponding operation is pause & edit % 1. asm ).
In addition, like the other two methods, this method can also be negative. The meaning of the above three sentences is still expressed in a negative form, and the code becomes:
Masm % 1.asm
If not errorlevel 1 link % 1.obj
Pause & edit % 1.asm
Have you seen the essence? In fact, the Command executed after the result is judged is exchanged. The effect of "if not errorlevel 1" and "if errorlevel 0" is equivalent, all indicate that the previous masm command is successfully executed (because it is an error judgment and the return code is 0, 0 indicates no, that is, this error does not exist, that is, the masm command is successfully executed ). Whether to add not here. Whether the error code is 0 or 1 is two issues worth consideration. Once the script fails to be matched, the error will surely occur. Therefore, you must be very familiar with it. How can we be profound? Exercise! Write a script by yourself, and write in the case that there is not and there is no not, And the return code is 0 or 1 respectively for execution (why is it too troublesome? You may be in trouble when sorting and combination is only four? It is more troublesome to introduce pipeline commands and combined commands later! Afraid? Haha .), In this way, we can clearly see the differences between the two situations from the execution results.
The use of errorlevel results is the most difficult to use the if command, but it is also the most useful usage. if you do not use errorlevel to judge the return code, the same effect will be achieved, else must be used to represent "otherwise" operations, which is quite troublesome. The above code must be changed:
Masm % 1.asm
If exist % 1.obj link % 1.obj
Else pause & edit % 1.asm
The usage of the if command is easy to understand, but the application is not necessarily so handy, mainly because of proficiency. Some may be a little surprised. Why didn't I give an introduction to the usage similar to the following three lines, because the following three lines explain its usage in the help of the if command. Anyone only needs one "if /?" As you can see, I don't have to pay more for it here. The more important reason is that I don't think it is clear that the people who read it may understand it, so I will introduce the above self-understanding of the if command. It must be noted that these three usage formats are different and cannot be changed, but they can actually be exchanged (thought in essence, these three usage methods are based on judgment. Philosophy teaches us how to view the essence of things through phenomena !). If you are interested, you can study it yourself.
IF [NOT] ERRORLEVEL number do command
IF [NOT] string1 = string2 do command
IF [NOT] EXIST filename do command
8. call
If you have learned assembly or C, you must know what the call command means. Here it actually means the same. In the batch processing script, the call command is used to call another batch processing script from one batch processing script. Example 8 (the default file names of the three scripts are start. bat, 10. bat, and ipc. bat ):
Start. bat:
......
CALL 10.BAT 0
......
10. bat:
......
ECHO % IPA %. % 1> HFIND. TMP
......
CALL ipc. bat IPCFind.txt
Ipc. bat:
For/f "tokens = 1, 2, 3 delims =" % I in (% 1) do call HACK. bat % I % j % k
Have you seen anything wrong? Didn't you see it? It's right if you don't see it. Actually, there's nothing wrong with it. What do you think! From the above two scripts, you can get the following information: 1. script calling can be used flexibly, cyclically, and repeatedly. 2. parameters can be used for script calls! I won't talk much about the first point. If you are smart, you should have a look at it. here we will talk about the second point.

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.