DOS batch file

xiaoxiao2021-03-06  50

There are three executables under DOS, which are EXE, COM, and BAT. Where EXE and COM files are binary, only the BAT file is in the form of text, you can read it directly. Therefore, the BAT file is much simpler than the above binary executable. These files contain a collection of DOS commands, usually called the batch file. Although the composition of the batch file is relatively simple, it is very large, and the use is relatively wide. For example, every time you do some of the same commands, you will feel very troubles, and if you are executed in the batch file, it is much easier. AutoExec.bat is a special batch file that is automatically run at DOS startup and plays a very big role in the system configuration. Therefore, to learn DOS, you must learn a good batch of files. The ordering of the batch file is introduced below and its use.

DOS comes with batch command:

@: Put this symbol in front of the other command in the batch file, the command itself will not be displayed. If the @echo off command is often used in the batch file.

Call: Call another batch file from a batch file, continue to perform the original batch file after calling. Usage: Call [batch file name] Note: You can also use the command / c command to complete the same operation.

Choice: Select commands. This is a DOS external command, but it is mainly used in batch files. After the choice command is executed, you will prompt the optional item, then select it by a button. Usage: choice: [/ c [:] Press key table] [/ n] [/ s] [/ t [:] Select value, second number] [Display text] where, / c represents the button, / N Indicates not to display the prompt information, / s indicates that the case-sensitive manner, / t indicates that a certain selection value defined in / c is automatically executed if there is no option in the approved time. The display text is the prompt information when the choice command is executed. The result will be represented by Errorlevel value.

Echo: Displays the specified information. Normally appear on the screen. If the Echo Hello will display the words of Hello on the screen. In addition, Echo ON | OFF is used to set whether the command itself is displayed when executed in the batch file. Echo off is the same as @, but it is a separate command, but cannot be placed before @.

For: Run the corresponding command for the specified file. Everyone knows that many of the commands support wildcards under DOS. And *, you can specify a batch of documents at a time, it is very convenient. However, not all DOS commands support wildcards, such as Type (file content display command) is not supported. There is no relationship with the for command, which is beneficial to make the Type command to display multiple files at a time. Usage: For% variable name in (File Set) DO Command [Command Parameter] Note: The above is the fixed form of the for command, the location of IN and DO must be correct, otherwise the syntax error will be prompted. If the for% f in (*. *) Do Type% F command can implement the type command once again display multiple files. Note:% f is the variable name, or can be replaced by% G, but must be consistent before and after. Replace it with %% F in the batch file.

Goto: Go to a certain label inside the batch file to execute. As you know, you often need to repeat or jump to a place in a place, such as the goto command in the Basic language. The goto command in the batch file can also complete similar features. Usage: goto [Name] where the label name is available, such as Hello, etc. Set the label ":" symbol, such as ": hello", when you use the goto hello command to continue to perform a batch file in the location where ": hello" is located. IF: Conditional judgment command. This is a very useful batch command. Usage 1: if [not] exist file name command [command parameter] Significance: If [not] There is a certain file will execute a command. Usage 2: if [NOT] Errorlevel error Returns the code command [Command Parameter] Significance: If the error returns the code [not] greater than or equal to the specified code will execute a command. Among them, Errorlevel means that the error returns a code, it is useful. For many DOS commands, these commands return different codes due to different results (such as performing success, failure, or by user interrupt, etc.), which will return different codes to represent different results. If the error error commands perform different commands based on this different result, it is usually used after a command. If IF Errorlevel 1 Echo OK! Indicates that "OK!" Is displayed on the screen if the current error returns the code greater than or equal to 1. Usage 3: if [NOT] String 1 == String 2 Command [Command Parameter] Significance: When the string 1 and string 2 [not] do one command when it is equal.

PAUSE: The execution of the temporarily discontinued and displayed the words "according to any key".

REM: Add an annotation. Used to increase file readability, will not be executed. It can also be replaced by ::

Shift: Change the location of the replaceable parameters in the batch file. Replaceable parameters are a special parameter that can only be used in batch files. These parameters are entered by the user when the batch command is executed. For example, we execute the DIR / S / W command, where DIR is the command name, / s and / w are its execution parameters. In the batch file, these command parameters will be given to the replaceable parameters, such as% 1, / W, which are encompassed, respectively, so that the class is pushed, and the command itself is given to% 0. The batch file is to operate using the parameters that can be entered when executed. For example, there is a batch file called myfile.bat, execute myfile.bat yes in the command line, and the value of% 0 is myFile.bat,% 1 is "Yes", which can be used in this batch file. The command determines the value of the parameters such as% 1, then performs different operations depending on the different values, such as if "% 1" == "YES" goto yes. The shift command does not bring any parameters, the execution result is the value of the value of% 0 into the value of the original% 1, and the value of the value of the value of the original% 1 becomes the value of the original% 2, and so on. Pay attention to its irreversibility. Because the running parameters at the time of batch file may be many, it may exceed 10, and the replaceable parameters can only range from% 0 to% 9. If you want to obtain the parameter value after the% 9, you can only use the shift command. At this time, the entire parameter column will push forward.

The above is the self-batch command of DOS, it can be seen that these commands are very small. To write more complex programs, it is obviously impossible to implement with the above commands. At this time, other practical batch tools are required, famous and practical, TestiF, String, ASET, BATCHMAN, WBAT, etc., can be downloaded in "Script Tool" in DOS Software Category Download. Using the above tools in the batch file can achieve a very powerful feature, even the functionality of many advanced language programs. So, if you want your editor to write a batch file, they are your good choice, not only have a detailed document, but also in development. Here's the production of the batch file. In fact, it is not difficult to make batch documents, as long as the method is mastered. As you have increased the number of hours, you will feel more and more easily.

First use a text editor, such as the Edit command or other editing tools of the DOS, such as Pedit, etc., create a blank file (of course, create it directly with the COPY CON command), then enter the function you want to complete Batch command. If you just want to perform a collection of DOS commands, enter a DOS command in each line in order. But if you want to complete some more complex operations, you need the above batch command or the above-mentioned batch tool.

The following is a batch applet from 1% slowly added to 100%, you can use it as a reference to write your own procedure.

@echo offbreak offclsset C = 0writext 10 1 Wait ... Be delay 4: loopwritext 10 9% C %%% completed.count CIF NOT% C% == 101 goto loopecho.set c = kpush / fbreak on

Among them, I used WriteXT, Count, Be, and Kpush, which were batch tools. Writext is an ECHO enhancement tool that can customize the results of the display. Count is a variable computing tool. If the value of C is 1, the value of C after COUNT C is 2. BE is a powerful batch enhancement tool with Norton Utilties 8.0. Kpush is a keyboard buffer tool.

In short, using the batch command and batch enhancement tool, we can compose many powerful batch files, everyone may wish to try. Many batch tools can be downloaded in the Script Tool. ------------------------------------------------ Editor: Zhang Qingzhangking@263.net QQ: 9365822 msn: zhangking@hotmail.com Personnel Home: http://www.why100000.com ("100,000 Why" Computer Learning Network) Other Website: http://sogo99.com ("Sogou 99" network portal) http://neteye.blogchina.com ("Meet" Blog China) 2004.12.17

转载请注明原文地址:https://www.9cbs.com/read-79813.html

New Post(0)