DOS batch file processing

Source: Internet
Author: User

There are three types of executable files in DOS: EXE, COM, and bat. Among them, the EXE and COM files are both in binary format. Only the BAT files are in text format and can be read directly. As a result, the content of the BAT file is much simpler than the preceding binary executable file. These files contain a collection of DOS commands, usually called batch files. Although the composition of batch files is relatively simple, it is widely used and widely used. For example, if you execute the same command every time, you will find it very troublesome, but it is much easier to execute it in a batch file. Autoexec. bat is a special batch processing file, which runs automatically at DOS Startup and plays a very important role in system configuration. Therefore, to learn dos well, you must learn how to process files in batches. The following describes the commands for batch processing and their usage.

The batch processing Command provided by DOS:

@: Place this symbol before other commands in the batch file. The command itself is not displayed during running.
For example, the @ echo off command is often used at the beginning of a batch of files.

Call: Call another batch file from one batch file. After the call, continue to execute the original batch file.
Usage: Call [batch file name]
Note: You can also use the command/C command to perform the same operation.

Choice: Select a command. This is a DOS external command, but it is mainly used in batch files.
After the choice command is executed, a prompt is displayed for the project to be selected. Select the project by pressing a key.
Usage: Choice: [/C [:] key table] [/n] [/S] [/T [:] Select a value, in seconds] [display text]
/C indicates an optional button,/n indicates that the prompt information is not displayed, And/s indicates the case-sensitive mode, /t indicates that if no selection is made within the batch time, a selection value defined in/C is automatically executed. The displayed text is the prompt information when the choice command is executed. The selection result is represented by the errorlevel value.

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

For: Run the corresponding command for the specified file.
As you know, many DOS commands support wildcards, such? And *. You can specify a batch of files at a time, which is very convenient. However, not all DOS commands support wildcards, such as type (file content display command. With the for command, it does not matter, so that the type command can display multiple files at a time.
Usage: For % variable name in (file set) do command [command parameter]
Note: The above is a fixed form of the for command. The in and do locations must be correct; otherwise, a syntax error will be prompted.
For example, you can run the for % F in (*. *) do type % F command to display multiple files at a time.
Note: % F is the variable name and can also be replaced by % G, but must be consistent before and after. Replace % F with % F in the batch file.

Goto: Switch to a label inside the batch file for execution.
As you know, in programming, it is often necessary to repeat or jump to a certain place to continue to execute, such as The Goto command in BASIC language. The Goto command in the batch file can also perform similar functions.
Usage: goto [label name]
The label names can be set at will, such as hello. Set the label with the ":" symbol, such as ": hello". Then, use the Goto Hello command to go to the location where ": Hello" to continue executing the batch file.

If: Conditional judgment command. This is a very useful Batch Processing Command.
Usage 1: If [not] exist file name command [command parameter]
Meaning: If [No] A file exists, a command is executed.
Usage 2: If [not] errorlevel error return code Command [command parameter]
Meaning: If the return code [No] is greater than or equal to the specified code, a command is executed.
Errorlevel indicates the return code of the error, which is useful. Many doscommands return different codes to indicate different results due to different execution results (such as successful execution, failed execution, or interrupted by users. The IF errorlevel command runs different commands based on different codes generated by different results. It is usually used after a command. For example, if errorlevel 1 echo OK! If the return code of the current error is greater than or equal to 1, "OK!" is displayed on the screen !" .
Usage 3: If [not] string 1 = string 2 Command [command parameter]
Meaning: execute a command when string 1 and string 2 [do not] are equal.

Pause: Pause the execution of batch files and display the words "press any key to continue.

Rem: Add annotation. It is used to increase file readability and will not be executed. It can also be replaced.

Shift: Change the positions of replaceable parameters in the batch file.
Replaceable parameters are special parameters and can only be used in batch files. These parameters are input by the user when executing the batch processing command. For example, run the Dir/S/W command, where DIR is the command name, And/s and/W are the execution parameters. In the batch file, these command parameters are assigned to replaceable parameters, for example, % 1 in/s, % 2 in/W, and so on, the command itself is granted to % 0. A batch file uses replaceable parameters to operate on the Parameters entered during execution. For example, a batch file is called myfile. BAT: Execute myfile in the command line. bat yes, so the value of % 0 is myfile. BAT, the value of % 1 is "yes". In this batch of files, you can use commands such as if to determine the value of parameters such as % 1, and then perform different operations based on these values, for example, if "% 1" = "yes" Goto yes.
The shift command does not contain any parameters. The execution result is to replace the value of % 0 with the value of the original % 1, and the value of the original % 1 is the value of the original % 2, and so on. Pay attention to its irreversible nature. Because there may be many running parameters in the execution of batch files, there may be more than 10, and the replaceable parameters can only be changed from % 0 to % 9. To obtain the parameter values after % 9, only the shift command can be used. The entire parameter column is pushed forward.

The above are the built-in batch processing commands of Dos. We can see that these commands are very few. To write more complexProgramObviously, it is impossible to use the above commands. In this case, we need other practical batch processing tools, including testif, String, aset, batchman, and wbat, all can be downloaded from "script Tools" in "DOS software download by category. The above tools can be used in batch processing files to implement very powerful functions, and can even complete the functions of many advanced language programs. Therefore, if you want to compile the batch processing files by writing them, they are a good choice for you. They not only have detailed documents, but are still developing.

The following describes how to create a batch file. In fact, it is not difficult to create a batch file. You only need to master the method. As the number of operations increases, you will find it easier.

First, use a text editor, such as the edit Command provided by DOS or other editing tools, such as pedit, to create a blank file (of course, use the copy con command to create a blank file ), then enter the Batch Processing Command Based on the function you want to complete. If you only want to execute a collection of some DOS commands, enter a DOS command in order in each line. However, if you want to complete more complex operations, you need the above batch processing commands or the above batch processing tools.

The following is a batch processing applet that I developed from 1% to 100%. You can use it as a reference to compile your own program.

@ Echo off
Break off
CLS
Set C = 0
Writext 10 1 wait...
Be delay 4
: Loop
Writext 10 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, which are all batch processing tools. Writext is an echo enhancement tool that allows you to customize the display effect. Count is a variable computing tool. For example, if the original value of C is 1, the value of C is 2 after Count C is executed. Be is a powerful batch processing Enhancement Tool available in Norton utilties 8.0. Kpush is a Keyboard Buffer tool.

In short, using batch processing commands and batch processing enhancement tools, we can compile many powerful batch processing files. You may try it. Many batch processing tools can be downloaded from the script tool.

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.