Windows batch processing commands

Source: Internet
Author: User
Tags echo command

A batch file contains one or more commands. Its file extension is. bat or. cmd. Using batch files simplifies routine repetitive tasks. Its help: Command /? Eg: ECHO /?

Press Ctrl + C to forcibly terminate the execution of a batch.

Echo command

---- Enable or disable the request echo function or display messages. Executing @ echo off not only disables the echo of subsequent commands, but does not display the echo off command itself. Generally, @ echo off is used as the first line of the batch processing program.

-- Echo Hello World


Goto and:

---- Specify jump to tag

@ Echo off

: Start

Set/a var + = 1

Echo % var %

If % var % Leq 3 goto start

Pause

Output: 1 2 3 4


Rem command

--- Equivalent to:, annotation command, which is <=>/**/in C language. It will not be executed, but is only used as a annotation for ease of reading and subsequent modification.

--- Rem hello world


Pause command

--- Pause to prevent exit after batch processing is completed! After the pause command is executed, the command "press any key to continue..." is automatically displayed in cmd.


Call Command

--- Call another fart processing program from one batch processing program without terminating the parent batch processing program.

--- Call path \ filename. bat


Start command

--- Call external programs. All DOS commands and command line programs can be called by the start command (call can only call batch files for a wider range than call ). A new cmd window will be created.

Start explorer d: \ call the image interface to open disk d

Choice command

--- Enable the user to enter a character for selection, and return different errorlevels based on the user's choice. Then, in combination with if errorlevel, run different commands based on the user's choice.

----- Sample. bat

@ Echo off

Choice/C dimethyl/M "defrag, mem, end"

If errorlevel 3 goto end rem (the highest error code should be determined first)

If errorlevel 2 goto mem

If errorlevel 1 goto defrag


: Defrag

C: \ dos \ defrag

Goto end


: Mem

Mem

Goto end


: End

Echo good bye


If command

--- Indicates determining whether the specified conditions are met and thus deciding to execute different commands. The format is 3.

A. if "parameter" = "string" command to be executed

----- If "2" = "2" echo 2 = 2

B. if exist file name command to be executed

C. if errorlevel/if not errorlevel number command to be executed


For command

--- Execute the command cyclically and specify % variable for the variable

For/l % I in (1, 1, 5) do @ echo % I

Pause ---- output 1 2 3 4 5


Errorlevel

--- Program return code.

Echo % errorlevel %. After each command is run, you can view the return code in the command line format to determine whether the command is successfully executed. The default value is 0. If an error occurs during command execution, it is set to 1.

Title

--- Set the title of the cmd window

Title Hello, you can see that the Title of the cmd window has changed

MD

-- Create a folder

Md d: \ mp6 create the mp6 folder in d: \

Command combination

1. & (and) the first command & the second command [& the third command...]

This method can be used to execute multiple commands at the same time, regardless of whether the command is successfully executed.


2. & (short circuit and) the first command & the second command [& the third command...]

This method can be used to execute multiple commands at the same time. When an error occurs, the subsequent commands will not be executed. If there is no error, all the commands will be executed;


3. | (or) The First Command | the second command [| the third command...]

This method can be used to execute multiple commands at the same time. When a correct command is executed, the subsequent commands are not executed. If no correct command is displayed, all the commands are executed;

Special symbols in Batch Processing

@, >,>>,|, ^ ,&,&&,"",,,;,

Variable
@ Echo offrem hello worldecho echo hello world: call calc.exe rem setting variable set var = 1234 rem output variable echo % var % pause


Reference: windows Batch Processing Command tutorial

Related Article

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.