Learn batch processing, hard learning is easy! Another good view 1th/3 page _dos/bat

Source: Internet
Author: User
Tags echo command error code goto tag name
This is a technical tutorial, sincerely will use very simple words to express their meaning, as long as you can read and understand, you can learn knowledge. The purpose of this tutorial is to let every friend who has read these words remember a word: if love can make things easier, then let it be easy! The way to see this tutorial is slow! Slowly, as a product of a woman, a cup of tea, you will find many before in front of things suddenly become very distant, and some very distant things but suddenly returned to the front.
Let's outline what the batch process is. The definition of batch processing, so far I have not been able to give a suitable----many experts have not given----anyway, I do not know----look at me is not necessarily convinced----I am a rookie, not to mention, of course, but I would like to sum up a "more appropriate", and I also believe that I can explain it very clearly , let more rookie all know this is what dongdong, you use this dongdong can do what thing. Perhaps you will because of this article and "Unconditional love batch", then my goal is reached----I just want to let you love it, I just so drag, what can you do?? Really, love sometimes so drag, is so no reason, is so don't face! It's true!
In my understanding, the nature of batch processing is a set of DOS commands that are arranged in a certain order.
Ok,never Claver and get to business (gossip less about it). Batch, also known as batch script, is translated in English to batch, batch file suffix bat to take the first three letters. Its composition has no fixed format, as long as the following is OK: Each row can be treated as a command, each command can contain multiple note commands, starting from the first line, until the end of the last line, it runs the platform is DOS. Batch processing has a very distinct characteristics: easy to use, flexible, powerful, high degree of automation. I do not want to write my own tutorial boring, because 牵缠 to the code (batch content is code?) The problem is boring, and few people can face the full screen of the code and calm down. So I will use a lot of simple and practical examples to read this tutorial friends to experience batch processing that the radiant charm, feel it that ancient spirit of the character, unconsciously fell in love with batch processing (halo, How is Love?) What does a batch deal have to do with love? Answer: No! )。 Again "gossip": To learn batch processing, DOS basic must be in prison! Of course, brain flexibility is also a very important aspect.
Example one, first give a most easy batch script let everyone and it face familiar with it, the following lines of command to save as Name.bat and then execute (in the next article only give code, save and execute 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
Is it possible to read? Is it easy? But its role is very practical, the implementation of this batch, you can build a current disk called A.txt file, it records information can help you quickly find the fastest QQ server, so far away from the "Relay from the server" that painful process. Here > means to put the things in the previous command to the place given in the,>>, and the same as >, the difference is to append the results to the previous line of the results of the result, specifically the next line, and the previous line of command results will be retained, This will make the A.txt file bigger and larger (think about how to sabotage it??) )。 By the way, this batch can also be combined with other commands, make it completely automated to determine the speed of the server, the implementation of direct display of the fastest server IP, is not very cool? It will be described in more detail later.
Second, give an example of an outdated case (A.bat):
@echo off
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 with advertising qq (KAO, my QQ also show friends measurements!!) ), so it's almost useless. But once its role is not small peep: Delete qq ads, let the dialog box clean. The address used here is the default installation address of QQ, the default batch file name is A.bat, you can of course modify according to the circumstances. The IF command is used in this script, so that it can be timely to determine and delete the effect of advertising pictures, you need to not close the command after the implementation of the DOS window, do not press CTRL + C forced to terminate the command, it has been monitoring whether there are advertising pictures (QQ also constantly check their ads are deleted). Of course this script takes up a little memory of you, hehe.
For example, use batch script to find out whether a middle glacier. The script reads 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, using the netstat command, check all the network port status, only you know the common Trojans used by the port, you can easily determine whether the race of the glacier. However, this is not certain, because the glacier default port 7626, can be modified completely. Here are just the methods and ideas. Here is the introduction of methods and ideas to make changes, you can check other Trojan script, and then change, add in parameters and port and Information list file, it becomes automatic detection of all Trojan script. Oh, is not very enjoyable? The script also uses the combo command && and Pipe command |, which is described in more detail later.
Example four, the batch processing automatically clears the system garbage, 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 the Autoexec.bat, every time when the system garbage to automatically delete. Here need to pay attention to two points: first, DOS does not support long filename, so there is tempor~1 this dongdong, two, can be changed according to their actual situation, make it meet their own requirements.
How about, see here, do you have a bit of interest in batch processing scripts? Do you find yourself falling in love with this stuff? Don't be happy too early, love is not a simple thing, it may bring you happiness and happiness, of course, can make you painful to jump. If you know it's hard to go on, I'm you! Keep working hard, maybe in the end you don't have to get true love (it's really possible, loved ones know), but you can feel the whole process of love, that's all. Sour, bitter and spicy, there is no sweet God knows.
Why does the batch process have anything to do with love? It's not that I'm bored, it's not because it's funny how much, there are two reasons: one, batch processing and love have a lot of the same place, some places I use the "professional" jargon is not clear (I do not doubt their ability to express, but the matter itself is not clear), said = Did not say, But a metaphor of love known to all on Earth (what is Love?) How do I know!! , maybe you will be bright in your heart, with half the effort, why not? Second, my time is not very good, cold fever headache nasal congestion, but the main or emotionally devastated, engaged in the people bored, borrow a few words of the time to write a tutorial, we all when buy Dogskin plaster, completely can omit not to look (perhaps really a little effect----not let you look at the fall asleep, Knock your head off and come to me for reimbursement of medical expenses). Perhaps next time in the tutorial you will see Yang, Zhang Mowgli and other Jin Veteran's heroes.
After reading the first chapter of the friends, must have a preliminary impression of batch processing, know what it is used to do. But do you know where the essence of batch processing is? Actually very simple: the idea must be nimble! Nothing can not be done, only unexpected. This and love is a little different, because the world of love is two of people's world, wishful thinking does not call Love (added: that is called unrequited affection.) Nonsense! and batch processing is a person's heaven, you can do whatever you want, not up to the realm!
Batch processing looks messy, but it's logical, absolutely no less than other programming languages (such as compilations), if you write a script that is a mess, and although every line of command is correct, but from the beginning to the end, not necessarily get the result you want, perhaps a screen of bad command or fail name. This has something in common with love: Steps to run, missing or increased steps can lead to a result that you don't want to see. Fall in love with friends, I believe there is no doubt this sentence. My Love batch processing, the output result is not bad command or fail name, the screen shows: ' Your Love ' is not an internal or external command, nor a running program or batch file. Then the cursor keeps flashing, waiting for the next wrong input.
Starting from this chapter, we will introduce the common commands in batch processing, many common DOS commands have this extensive application in batch script, they are the body part of batch script, but batch processing is more flexible and more automation than DOS. To learn batch processing, DOS must have a relatively solid foundation. Here only some relatively less use (relatively) DOS commands, commonly used commands such as copy, dir, etc. do not introduce (these seemingly simple commands are actually complex, I am afraid I can not say clearly!) )。
Example five, look at an instance first. This is a very interesting script, a small and practical good dongdong, the batch processing "Automation" features reflected incisively and vividly. First introduce the origin of this script: we all know Assembler (MASM) of the machine process, first to the source code to compile, connect, and then execute, and there are many links between the need to enter a lot of things, the trouble is very (only experienced friends understand). How to make this process simple? When we compile the curriculum design, I "was forced" to write this script, use it very cool, hehe. Look at the script content:
@echo off
:: Close Echo
:: Clean screen
echo this programme are to make the MASM programme automate
::d isplay Info
Echo Edit by codered
::d isplay Info
echo Mailto me:qqkiller*** @sina. com
::d isplay 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
::p ause to usage
:: Assemble the. ASM Code
if errorlevel 1 pause & Edit%1.asm
:: If error pause to = 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
::d isplay usage
Do not be scared by this pile of things, calm down to look carefully (recall the first chapter in the first paragraph is how to write!!) )。 has given the explanation of each line of command, two colon after the content of the first line of content to explain E-wen (Afraid of E-Wen's friends do not worry, are very easy, a look will understand, really do not understand the dictionary ah, so lazy? , and does not show up when the script is executed, nor does it have any effect. At the beginning of the 5th line there is a colon, not a clerical error. The specific effect will be described in detail later. In this script MASM and link are assembler and link programs that must and edit programs as well as the source code you want to edit (and, of course, this script, nonsense!). ) together in the current directory. Using this batch script, can be the most possible reduction of manual input, the whole process only need to press a few hit enter, you can achieve from the assembly source code to executable EXE file automation conversion, and have intelligent judgment function: if the assembly when the source code error (assembly is unsuccessful), then automatically suspend the display error message, And after pressing any key to automatically enter the editing source code interface, if the source code compilation is successful, connect, and automatically execute the generated EXE file after the connection. In addition, due to the simplicity and flexibility of the batch command, this script is also good to improve, simple to modify to meet the different friends of the machine habits. is learning the compilation of friends, must not forget the internship!
There are several commands in this script: @, Echo,::, pause,: and Goto,%, and if. And this chapter will cover these several orders.
1, @
This symbol is not unfamiliar to everyone, email the necessary symbol, how it will run to batch processing it? Oh, not its fault, batch processing is inseparable from it, or it is not perfect. Its function is to let the execution window not show the command itself in the line behind it (how to wrap a word!). )。 Oh, popular point of saying, at the beginning of the word, the line of command will not show. In the example V, in the first line of @echo off, the @ function is to have the script not display the following echo off part when executing. Do you understand? Still don't understand? Never mind, read the echo command introduction, Nature will understand.
2, Echo
Chinese is the meaning of "feedback" and "echo". It is actually a switch command, which means that it has only two states: open and close. So there's the echo on and echo off two commands. Executing the echo command directly displays the current ECHO command state (off or on) performing echo off to turn off echoing, and all subsequent commands do not display the command itself, only the result of execution, unless the echo on command is executed. In Example V, the @ command for the first line and the echo off command are combined for two purposes: the echo off command itself is not displayed, and the commands themselves in subsequent lines are not displayed. It's a bit messy, but if you practice, 3 minutes will not be a refund!
Another use of the echo command: You can use it to display information! For example v the penultimate line, the Default BAT file name is START. BAT will appear in the window after the script executes, and the echo command itself does not display (why??). )。
Another use of the echo command two: You can edit a text file directly. Example SIX:
echo nbtstat-a > A.bat
echo nbtstat-a >> A.bat
echo nbtstat-a >> A.bat
The above script content is edited by direct command line input, one back to each line. Finally, a A.bat file is generated in the current directory, and direct execution results.
The function of this command is simple, which is the annotation command, which is equivalent to the REM command in the batch script. The content behind it does not appear, nor does it work, because it is only a comment, but it increases the readability of the script, similar to the/*............*/in the C language. The Earth people can understand, do not say more.
4, pause
Chinese is the meaning of "pause" (look at your workman), I always think it is the simplest order in batch processing, simple, practical. Its role is to let the current program process pause and display a line of information: Please press any key to continue .... In the case of v this command was used two times, the first role is to let users see the program information, the second is to display the wrong assembly code information (in fact, it does not want to display, but the MASM program in the display of error messages are temporarily stopped it, so that you can see clearly your source code is wrong where).
5.: And Goto
Why combine these two commands together? Because they are inseparable, no matter which one or more of them will go wrong. Goto is a jump command: is a label. When the program runs to Goto, it will automatically jump to: the defined part is executed (is it inseparable?). )。 Example v the 5th line at the beginning of the countdown. A: The program automatically jumps to the part of the label definition when it runs to goto: The result is to display the script usage (usage is the tag name). It is not difficult to see that the goto command is based on this colon and tag name to find where it should jump, they are one by one corresponding relationship. Goto commands are often used in conjunction with the IF command. As for the specific use of these two commands, refer to example five.
Another use of goto command one: End the program prematurely. Use the goto command to jump to a label in the middle of a program, but the content of the label is defined as exiting. Such as:
Goto END
: End
Here: End in script last line! In fact, this example is very retarded, then the IF command and the combination of commands you know.
This percent sign is strictly not a command, it is only a batch of parameters (except for the use of multiple%, in the future will be described in detail), but do not think it is just a parameter to underestimate it (see example v how many places to use it?) Less than 51% of the functionality of the batch is reduced. See example Seven:
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, and% 1 represents the ip,2% representative of username,3% password. Execution form is: script filename parameter two .... Assuming that the script is saved as A.bat, the execution form is as follows: a IP username password. Here IP, username, password is three parameters, integral (because the program does not run correctly, not because of the lack of parameter syntax is not correct) so in the script execution, the script will automatically use your three parameters in turn (remember, is in turn!). is also one by one the corresponding relationship. ) substitution 1%, 2% and 3%, thus achieving the purpose of flexible use (imagine, if the script directly to the IP, username and password are defined dead, then the role of the script will be fixed, but if the use of%, different parameters can achieve different purposes, is not more flexible? )。
The use of this parameter is also described in the following sections. Must be very skilled to do, this requires a lot of practice process, need to do a little hard work!
This chapter is written here. May have a friend asked: How did not introduce if command? Oh, not I forget, but it is not easy to say clearly, the next chapter again! This chapter, if you are a beginner, I'm afraid it's enough to digest. Remember one thing: DOS is the body of a batch, and any DOS command can be used in a batch script to accomplish a particular function. Here, have you ever thought of using your belly to write something with an automatic color? It's simple, it's a set of DOS commands, and I believe that you've been able to automate the DOS portion of a computer-grade exam with a batch process.
Bother! Just like a half old woman to menopause, anything want to nag a few words, what things are uncomfortable, see who is unhappy. Mingchi, undeterred, finally left a scar without work and return, only to find himself so fragile, so small, so vulnerable. Wandering on the verge of collapse, suddenly think back to their last time to flat people that moment, really a little miss (in fact, I do not like to flat people, but also do not like to be flat). I need to vent, I use my fingers to beat the keyboard desperately, in a burst of rhythmic sound, the screen appears on the above text. But is this another way to vent? The Chinese are still fierce, as early as thousands of years ago Confucius said "only women and the villain, difficult to raise also", true * * has foresight, admire! Although is in vent, but everybody please rest assured, with my temper, since decided to write this tutorial, will certainly try to write well, writes the perfect, absolutely does not give oneself to leave the regret, otherwise this tutorial is not I writes!
There was a classic batch tutorial on your screen, you did not save, until you can not find its link you regret, the world's greatest pain is this. If God can give you a chance to see again, you will say three words to the tutorial: I love you! If you have to add a deadline for this love, you want to be 100 years. Because 100 years later, you're probably already dead! And now, on your screen, this batch tutorial that you're looking at is not as good as the classic you've been looking at, but if you're going to have to. Will you love it? Is it going to be as long as 50 years? The answer is: try it.
Some of the most important commands in the batch script will be covered in detail in this chapter, but unfortunately, some of the details have not been well mastered, even some zoning. Just as I don't know much about love. But I've been trying, even though I've never had a harvest. So it may be more general, but I will tell you the method, the rest is time problem, need to hone their own. Let's work together. Rome overnight, hollows is not a day's work. Some things, such as learning batch processing, such as love a person, are not a crash, and even have to pay hard and harvest for very little situation. Again, when you look at this tutorial, be sure to calm down, unless you have mastered everything in this tutorial----but that doesn't have to be seen, wasting time!
7, if
Follow the previous chapter and then speak the IF command. In general, an if command is a command that represents judgment, and according to each result, it can correspond to a corresponding operation. There are three ways to use it, say it separately here.
(1), input judgment. Or a few of the sentences in use case five:
If "%1" = "" Goto usage
If '%1 ' = = '/? ' Goto usage
If '%1 ' = = ' help ' goto usage
This determines the parameters of the input, if the parameter is null (no parameters), then jumps to the usage, if the parameter is/? or help when (we generally look at an order of assistance, is not the input/? or help, this is done just to make the script look more like a real program. Also jump to usage. Here you can also use the negative form to indicate "not equal to", for example: If not "%1" = "" goto usage, if the input parameter is not NULL to jump to usage (in practice this does not make sense, here to introduce usage, tube not so much, hehe.) Is it simple? In fact, the translation into Chinese experience is understand.
(2), existence judgment. Take another look at the sentence in the second example:
If exist C:\progra~1\tencent\ad\*.gif del c:\progra~1\tencent\ad\*.gif
If those GIF files exist, delete the files. Of course, there are four cases, the same reason. Note that the conditional judgment here is to judge the existence, and of course it can be judged that it does not exist, such as the following sentence "Exit script if there are no GIF files": If not exist c:\progra~1\tencent\ad\*.gif exit. Just one more not to express the negation.
(3), the result judgment. Or take the case of five surgery (did not think of their own script, unexpectedly useful so big, hehe):
if errorlevel 1 pause & Edit%1.asm
The source code is compiled first, if the failure to suspend the display error message, and press any key to automatically enter the editing interface, otherwise use the link program to connect the generated obj file. Here is a brief description of where the if order relates to the,& command. This usage is the first to determine the return code after the execution of the previous command (also called the error code, the DOS program has a return code after it is run, and if the error code defined is in line with the error code defined here (1), perform the appropriate action (the corresponding action here is the Pause & Edit%1.asm section).
In addition, as with the other two usages, this usage can also express negation. The meaning of the above three sentences is still expressed in the form of negation, and the code becomes:
If not errorlevel 1 link%1.obj
Pause & Edit%1.asm
Do you see the essence? In fact, the results of the judgement after the implementation of the order of the exchange, "if not errorlevel 1" and "if errorlevel 0" effect is equivalent to the previous sentence MASM command execution success (because it is error judgment, and the return code for 0,0 is negative, It means that the error does not exist, that is, MASM execution succeeds. Whether to add not here, the error code in the end with 0 or 1, is worth considering two issues, once the match does not succeed in the script is sure to go wrong, so be sure to experience a very deep before the line. How to realize the profound? Practice! Write a script yourself, and then write it in the case where there is not and not, and the return code is 0 or 1, respectively. Arrange the combination of the four in the case you're in trouble? There is even more trouble when it comes to piping commands and combination commands. Are you scared? Oh. So that the difference between the two situations can be clearly seen from the results of the execution.
This use of ERRORLEVEL results to judge is the most difficult use of the if command, but it is precisely the most useful use, if you do not use ERRORLEVEL to judge the return code, you have to achieve the same effect, you must use else to express the "otherwise" operation, is more troublesome. The above code must become:
If exist%1.obj link%1.obj
else Pause & Edit%1.asm
The three usages of the IF command are say here, the understanding is simple, but the application is not necessarily used so handy, mainly is the proficiency of the problem. Maybe some of my friends were a little surprised how I didn't give a description of the following three lines, because the following three lines are an explanation of its own usage in the IF command help, anyone who only has an "if/?" I can see, I do not need to spend a lot of breath here, more important reason, is that I think this introduction is not clear, look at people do not necessarily read, so I use the above my own understanding of the if command to introduce. It is important to note that these three usages of the format are different, but also can not be changed, but in fact interchangeable (thought that in essence, these three usages are based on the basis of Judgment, philosophy teaches us to learn to see things through the phenomenon of nature!) )。 Interested friends can study for themselves.
IF [NOT] ERRORLEVEL number do command
IF [NOT] string1==string2 do command
IF [NOT] EXIST the filename do command
8, call
A friend who has learned the assembly or C must know what the call instruction means, and it means the same thing here. In batch scripts, the call command is used to invoke another batch script from one batch script. See example eight (the default three script filenames are Start.bat, 10.bat, and Ipc.bat):
Call 10.BAT 0
ECHO%ipa%.%1 >hfind. Tmp
Call Ipc.bat IPCFind.txt
for/f "tokens=1,2,3 delims="%%i in (% 1) does call Hack.bat%%i%%j%%k
Did you see anything wrong? Didn't you see it? Did not see the right, in fact there is no wrong place, how do you see it out! From the above two scripts, you can get the following information: 1, script calls can be flexible to use, recycling, reuse. 2, script calls can use Parameters! On the 1th is not much to say, smart you see it should be, here to say the 2nd.
Current 1/3 page 123 Next read the full text

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.