BAT file (batch file) syntax

Source: Internet
Author: User
Tags echo command

Common commands and parameters for BAT Batch File Syntax

Common commands

Echo, @, call, pause, and REM (TIPS: Use: instead of REM) are several of the most common commands for batch file processing. We started from them.

===== Note ==============
First, @ is not a command, but a special identifier for DOS batch processing. It is only used to block command line echo. the following are some special tokens that may be seen in the doscommand line or batch processing:
Cr (0d) command line terminator
Escape (1B) ANSI Escape Character Guide
Space (20) Common parameter delimiters
Tab (09); = uncommon parameter delimiters
+ Copy command file Connector
*? File wildcard
"" String Separator
| Command pipeline operator
<>>> File redirection
@ Command line echo Shield
/Parameter switch Guide
: Batch tag pilot
% Batch variable pilot

Second,: it can indeed play the role of REM annotation, and it is more concise and effective; but there are two points to note:
First, in addition to:, any character line starting with: is regarded as a label in batch processing, and all subsequent content is directly ignored, to distinguish it from a normal label, we recommend that you use a label that is not recognized by Goto, that is, a special symbol that is followed by a non-alphanumeric character.
Second, unlike REM, the following character lines are not displayed during execution, whether or not echo on is used to open the command line echo status, because the command interpreter does not regard it as a valid command line, Rem is more suitable than: In some cases. In addition, REM can be used in config. sys File.
==================================

Echo indicates the characters after this command
Echo off indicates that after this statement, all running commands do not display the command line itself.
@ Is similar to echo off, but it is added at the beginning of each command line, indicating that the command line of this line is not displayed at run time (only the current line can be affected ).
Call calls another batch file (if you directly call another batch file without calling it, you will not be able to return the current file and execute subsequent commands of the current file after the batch file is executed ).
Running pause will pause the execution of batch processing and display press any key to continue... Prompt, wait for the user to press any key to continue
Rem indicates that the character after this command is interpreted as a line (comment). If it is not executed, it is only used for future reference (equivalent to a comment in the program ).
===== Note ======
The description here is confusing. It is better to reference the command line of a command to make it more organized.

---------
Echo

When the program is running, the body of the batch processing program is displayed or hidden. It can also be used to allow or disable command echo.

The MS-DOS typically displays (ECHO) commands in the batch processing program on the screen when running the batch processing program.
Use the echo command to disable this function.

Syntax

Echo [ON | Off]

If you want to use the echo command to display a command, you can use the following syntax:

Echo [Message]

Parameters

On | off
Specifies whether to allow command echo. To display the current echo settings, you can use echo without parameters.
Command.

Message
Specifies the body of the MS-DOS to display on the screen.

-------

Call

Calling another batch processing program from one batch processing program will not result in the suspension of the first batch processing.

Syntax

Call [drive:] [path] filename [batch-parameters]

Parameters

[Drive:] [path] filename
Specifies the name and storage of the batch processing program to be called. The file name must use. BAT as the extension.


Batch-Parameters
Specifies the command line information required by the batch processing program.

-----------

Pause

Pause the execution of the batch processing program and display a message prompting you to press any key to continue execution. Only in batch
Program.

Syntax

Pause


Rem

Add annotations to the batch file or config. sys. You can also use the REM command to shield the command (in config. sys
You can also use semicolons (;) instead of the REM command, but it cannot be used in batch files ).

Syntax

Rem [String]

Parameters

String
Specifies the command to be blocked or the annotation to be included.
======================================

Example 1: edit. BAT file, enter the following content and save the disk as c: \. bat, after executing the batch processing file, you can: write all the files in the root directory into a.txt, start UCDOS, enter WPS and other functions.

The content of the batch file is: Command comment:

@ Echo off does not display subsequent command lines and current command lines
Dir C: *. *> a.txt: Write the C-drive file into a.txt.
Call c: \ UCDOS. Bat call UCDOS
Echo Hello show "hello"
Pause pause and wait for the button to continue
Rem prepare to run WPS Note: Prepare to run WPS
Cd ucdos enters the UCDOS directory
WPS running WPS

Batch File Parameters

The batch file can also use parameters (equivalent to the command line parameters of the doscommand) like C functions, which requires a parameter identifier "% ".

% [1-9] indicates a parameter. A parameter is a string separated by spaces (or tabs) after the file name when a batch file is run. Variables can be changed from % 0 to % 9.% 0 indicates the Batch Processing Command itself. Other parameter strings are represented in the order of % 1 to % 9.

Example 2: C: the root directory contains a batch of processing files named F. bat with the following content:
@ Echo off
Format % 1

If c: \> f:
When F. bat is executed, % 1 indicates a:. In this way, format % 1 is equivalent to format A:. Therefore, the preceding Command actually runs format:

Example 3: C: the name of the processing file under the root directory is T. bat, and the content is:
@ Echo off
Type % 1
Type % 2

Then run c: \> T a.txt B .txt
% 1: paia.txt
% 2: Invalid B .txt
The following command displays the.txt and B .txt files in sequence.

===== Note ==========================
Parameters are also processed as variables in batch processing. Therefore, the percent sign is also used as the pilot character, followed by a number in 0-9 to form a parameter reference character. the relationship between the quote and the parameter (for example, the relationship between % 1 and A:) is similar to the relationship between the variable pointer and the variable value. to reference 11th or more parameters, you must move the start pointer of the DOS parameter. the shift command is acting as the pointer to move the starting pointer of the parameter to the next parameter, similar to pointer operations in C. the figure is as follows:

Initial status. CMD is the command name and can be referenced with % 0.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
|
% 0% 1% 2% 3% 4% 5% 6% 7% 9

After one shift, CMD cannot be referenced.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
|
% 0% 1% 2% 3% 4% 5% 6% 7% 9

After two shifts, arg1 is also deprecated, and % 9 points to null, with no reference significance.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
|
% 0% 1% 2% 3% 4% 5% 6% 8

Unfortunately, both Win9x and DOS do not support shift inverse operations. shift supports the/n parameter only in the NT kernel command line environment. You can use the first parameter as the benchmark to move the start pointer.

BAT file (batch file) syntax

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.