DOS Batch processing commands detailed

Source: Internet
Author: User
Tags continue execution 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.

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: 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.