DOS Batch File _dos/bat

Source: Internet
Author: User
Tags goto

There are three executable files in DOS, respectively, exe,com and bat. Among them, exe and COM file are binary form, only bat file is text form, can read directly. As a result, the bat file is much simpler than the binary executable file. These files contain a collection of DOS commands, usually called batch files. Although the composition of batch files is relatively simple, but its use is very large, and more widely used. For example, every time you execute some of the same commands, you will be in a lot of trouble, but it's much easier to do it in a batch file. AUTOEXEC. BAT is a special batch file, which runs automatically when DOS is started, and plays a very important role in the configuration of the system. Therefore, to learn DOS, we must learn batch processing documents. The following describes the commands for batch files and their use.

DOS self-brought batch command:

@ : Put this symbol in front of the other commands in the batch file, the runtime will not display the command itself.
If the @echo off command is often used in the first batch of files.

Call: Invokes another batch file from one batch file, and continues executing the original batch file after the call is completed.
Usage: call [batch filename]
Note: You can also use the COMMAND/C command to complete the same operation.

CHOICE : Select command. This is a DOS external command, but it is mainly used in batch files.
When the choice command is executed, it prompts for a selectable item, which is selected by a key.
Usage: choice:[/c[:] key sheet] [/n] [/S] [/t[:] Select value, seconds] [show text]
Where the/C indicates an optional key,/n means not to display the prompt,/s for uppercase and lowercase character sensitivity,/t means that a selected value defined in/C is automatically executed if there is no selection within the time of the approval. The display text is the prompt message when the choice command executes. The selection results are represented by ERRORLEVEL values.

ECHO : Displays the specified information. Usually appears on the screen.
such as echo hello will display the word hello on the screen.
In addition, ECHO on| Off is used to set whether the command itself is displayed when a batch file is executed. echo off is the same as @, but it is a separate command and cannot be placed before other commands like @.

for: Runs the appropriate command for the specified file.
As you know, many commands in DOS support wildcard characters, such as? and *, you can specify a batch of files at a time, very convenient. However, not all DOS commands support wildcard characters, such as type (the file content Display command) is not supported. It's okay to have a for command, which makes it possible for the type command to display multiple files at once.
Usage: for% variable name in (file set) do command [command arguments]
Note: The above is a fixed form for the for command, the in and do positions must be correct, or a syntax error will be prompted.
such as the For%F in (*.*) do type%F command enables the type command to display multiple files at a time.
Note:%f is a variable name, can also be replaced with%g, but must be consistent. Replace with%%f in the batch file.

goto : Go to a label within a batch file to execute.
As you know, in programming, you often need to repeat or jump to a place to continue, such as the Goto command in basic language. The goto command in the batch file can also perform similar functions.
Usage: GOTO [Label name]
Among them, the label name can be set arbitrarily, such as Hello. Set the label with the ":" Symbol, such as ": Hello", when the goto Hello command will go to ": Hello" where the batch file continues to execute.

IF : Conditional judgment command. This is a very useful batch command.
Usage 1:if [NOT] EXIST filename command [command parameters]
Meaning: If a file is not present it will execute a command.
Usage 2:if [NOT] ERRORLEVEL error return code command [command parameters]
Meaning: If the error returns a code [not] greater than or equal to the specified code name, a command is executed.
Among them, ERRORLEVEL represents the error return code, very useful. For many DOS commands, these commands return different code names to represent different results, depending on the results of the execution (such as execution success, execution failure, or interruption by the user). The IF ERRORLEVEL command performs different commands on different codes that are based on this different result, usually after a command. if errorlevel 1 ECHO ok! indicates that "ok!" is displayed on the screen if the current error returns a code greater than or equal to 1 o'clock The words.
Usage 3:if [NOT] string 1== string 2 command [command arguments]
Meaning: Executes a command when string 1 and string 2[are not equal.

PAUSE : Suspends execution of batch files and displays the words "Press any key to continue".

REM : adding annotations. Used to increase file readability and will not be executed. You can also use:: To replace.

shift : Changes the position of replaceable parameters in the batch file.
A replaceable parameter is a special parameter that can only be used in a batch file. These parameters are entered by the consumer 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 batch files, these command parameters are assigned to replaceable parameters, such as/s,%1,/w can be%2, and so on, and the command itself is assigned to%0. A batch file is an operation that uses replaceable parameters for the parameters entered at execution time. For example, there is a batch file called Myfile.bat, which executes Myfile.bat YES at the command line, and the value of%0 is the value of myfile.bat,%1 is "YES", which can be used to determine the values of parameters such as%1, in this batch of files. Then perform different operations based on the values, such as if "%1" = = "Yes" GOTO yes.
The shift command takes no arguments, and the result is that the value of%0 is replaced with the value of the original%1, and the value of the original%1 becomes the value of the original%2, and so on. Notice the irreversibility of it. Because a batch file may run with a lot of running parameters, possibly more than 10, and replaceable parameters can only be from%0 to%9, you can use the shift command only if you want to get the parameter values for%9. At this point, the entire parameter column is pushed forward.

The above is the DOS of the batch command, you can see that these commands are very few, to write more complex programs, the above command is obviously impossible to achieve. In this case, other practical batch tools are needed, the famous and practical Testif,string,aset,batchman,wbat, etc., can be downloaded in the scripting tools in the DOS software category download. Using the tools above in a batch file can be very powerful and can even accomplish the functions of many high-level language programs. So, if you want to write an editor's note of batch files, they are a good choice for you, not only with detailed documentation, but also in the ongoing development.

Next, we will talk about the production of batch processing documents. In fact, it is not difficult to make batch processing documents, as long as the master method on the line. As you increase your number of operations, you will find it more and more easy.

First use a text editor, such as a DOS-only edit command or other editing tool, such as Pedit, to create a new blank file (which, of course, is created directly with the Copy Con command), and then enter a batch command based on the functionality you want to complete. If you just want to perform a collection of DOS commands, enter a DOS command on each line in order. But if you want to do something more complex, you'll need the above batch command or the batch processing tool described above.

The following is a batch process that I originally compiled from 1% to 100%, and you can use it as a reference to write your own program.

@echo off
Break off
Cls
Set c=0
Writext 1 Wait ...
Be delay 4
: Loop
Writext 9%c%%%?completed.
Count C
If not%c%==101 Goto loop
Echo.
Set c=
kpush/f
Break on

Among them, I used writext,count,be and Kpush, these are batch processing tools. Writext is the Echo enhancement tool that can customize the display effect. Count is the calculation tool for a variable, such as the value of the original C is 1, and the value of C after the execution of Count C is 2. Be a powerful batch enhancement tool, with Norton Utilties 8.0. Kpush is the keyboard buffering tool.

In short, with batch commands and batch enhancement tools, we can make a lot of powerful batch files, so let's try. Many batch tools can be downloaded in the Scripting tools.

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.