Batch Processing Command Learning

Source: Internet
Author: User

1. Batch Processing: Automatic continuous execution of multiple commands.


2. Simple and common batch processing commands:

1. Echo indicates the character after the command is displayed, and echo off indicates that all running commands after this statement do not display the command itself;

2. @ 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 ).

3. 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 ).

4. Running pause will pause the execution of the batch processing and display the prompt "press any key to continue..." on the screen. Wait until the user presses any key to continue.

5. Rem indicates that the character after this command is interpreted as a line (comment). If it is not executed, it will only be used for future reference (equivalentProgram).


3. parameters can also be used in batch processing, with the 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.


4. Special commands

1. If is a condition statement used to determine whether the specified conditions are met and then decide to execute different commands. There are three formats:

1) if [not] "parameter" = "string" command to be executed

If the parameter is equal to the string specified by (not indicates unequal, the same below), the condition is true. Run the command; otherwise, run the next sentence.

Example: If "% 1" = "A" format:

2) If [not] exist [path \] command to be executed in the file name

If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence.

For example, if exist c: \ config. sys Type C: \ config. sys

Indicates that if the c: \ config. SYS file exists, its content is displayed.

3). If errorlevel <number> command to be executed

Many DOS Programs return a numeric value to indicate the result (or status) of the program running after the execution ends. The if errorlevel command can be used to determine the return value of the program, different commands are executed based on different return values (the return values must be sorted in ascending order ). If the return value is equal to the specified number, the condition is true. Run the command. Otherwise, run the next sentence.

For example, if errorlevel 2 goto X2

2. when the Goto batch file runs here, it will jump to the label specified by Goto (label, which is defined by the standard string). The GOTO statement is generally used with the if statement, execute different command groups according to different conditions.

For example:

Goto end

: End

Echo this is the end

The label is defined by ": string". The row where the label is located is not executed.

3. choice uses this command to allow the user to enter a character (used for selection), so that different errorlevels are returned based on the user's choice, and then run different commands according to the user's choice in combination with if errorlevel.

Note: Choice commands are external commands provided by DOS or windows. the syntax of choice commands of different versions is slightly different. Use choice /? View usage.

Choice command syntax (this syntax is the syntax of the choice command in Windows 2003, the syntax of choice in other versions is similar to this ):

Choice [/C choices] [/n] [/CS] [/T timeout/d choice] [/M text]

Description:

This tool allows you to select a project from the selection list and return the index of the selected project.

Parameter List:

/C choices specifies the list of options to be created. The default list is "YN ".

/N hide the Option List at the prompt. The preceding message is displayed, and the option is still enabled.

/CS allows you to select the case sensitivity option. By default, this tool is case-insensitive.

/T timeout specifies the number of seconds before the default value is set. The acceptable value ranges from 0 to 9999. If 0 is specified, there will be no pause. the default option is

Select.

/D choice: Specify the default option after NNNN seconds. The characters must be in the selection of a group specified by the/C option, and the/T must be used to specify the NNNN.

/M text specifies the message to be displayed before the prompt. If not specified, only the prompt is displayed.

/? Displays help messages.

Note:

The errorlevel environment variable is set to the key index selected from the selected set. Returns 1 for the first choice listed, 2 for the second choice, and so on. If the key you press is not a valid choice, the tool generates a warning. If the tool detects an error, it returns the errorlevel value of 255. If you press Ctrl + break or Ctrl + C, the tool returns the errorlevel value of 0. When the errorlevel parameter is used in a batch program, the parameters are sorted in descending order.

4. For Loop Command, as long as the conditions are met, it will execute the same command multiple times.

Syntax:

Execute a specific command on each file in a group of files.

For % variable in (SET) do command [command-parameters]

% Variable specifies a single letter replaceable parameter.

(SET) specifies one or more files. Wildcard characters can be used.

Command specifies the Command executed on each file.

Command-parameters specifies a parameter or command line switch for a specific command.

For example, a batch file contains one row:

For % C in (*. bat *. txt) do type % C

The command line displays the contents of all files with the bat and TXT extensions in the current directory.

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.