Use batch file-Common commands

Source: Internet
Author: User
Tags echo command
A batch file is a non-formatted text file that contains one or more commands. Its file extension is. bat or. cmd. In the command prompt, click the name of the batch file. When you double-click the batch file, the system will call cmd.exe to run them one by one based on the order in which commands appear in the file.

1. Introduction to simple batch processing internal commands

1. Echo command

Enable or disable the request echo function or display messages. If no parameters exist, the echo command displays the current echo settings.

Syntax

Echo [{on off}] [Message]
Sample: @ echo off/ECHO Hello World

In actual application, we will combine this command with the redirection symbol (also known as the pipeline symbol) to input some commands to a file in a specific format. this will be reflected in future examples.

2. @ command

The command after @ is not displayed. In the intrusion process (for example, you can use batch processing to format the enemy's hard disk), the other party cannot see the command you are using.

Sample: @ echo off
@ ECHO now initializing the program, please wait a Minite...
@ Format X:/Q/u/AutoSet (the/y parameter cannot be used for the format command. Fortunately, Microsoft has reserved the AutoSet parameter for us, the effect is the same as that of/Y .)

3. Goto command

Specify to jump to the tag. After the tag is found, the program processes the commands starting from the next line.

Syntax:

Goto label (label is a parameter that specifies the rows in the batch processing program to be switched .)
Sample:
If {% 1 }={} goto noparms
If {% 2 }={} goto noparms (if you do not understand the IF, % 1, and % 2 here, skip the step first, which will be explained in detail later .)
@ REM check parameters if null show usage
: Noparms
Echo usage: monitor. Bat serverip portnumber
Goto end

The name of a tag can start at will, but it is better to have a meaningful letter. Add a letter before it to indicate that the letter is a tag. The Goto command is based on this: to find the next step and jump to it. It is better to have some explanations so that others seem to understand your intention.

4. Rem command

The annotation command is equivalent to/* -------- */in the C language. It is not executed, but serves as a comment for others to read and modify.

Rem message
Sample: @ REM here is the description.
5. Pause command

When running the pause command, the following message is displayed:

Press any key to continue...
Sample:
@ Echo off
: Begin
Copy A: *. * D: \ back
Echo please put a new disk into driver
Pause
Goto begin

In this example, all files on drive a are copied to drive D: \ back. When the displayed note prompts you to put another disk into drive a, the pause command will suspend the program so that you can change the disk and press any key to continue processing.

6. Call Command

Call another batch processing program from one batch processing program without terminating the parent batch processing program. The Call Command accepts the labels used as the call target. If a call is used outside a script or batch file, it does not work in the command line.

Syntax

Call [[drive:] [path] filename [batchparameters] [: Label [arguments]

Parameters

[Drive:} [path] filename

Specifies the location and name of the batch processing program to be called. The filename parameter must have the. bat or. CMD extension.

7. Start command

Call an external program. All the DOS commands and command line programs can be called by the START command.

Common Intrusion parameters:

Minimum window size when Min starts
Separate starts a 16-bit windows program in a separate space
High starts applications in the high priority category
Realtime starts applications in the realtime priority category
Wait starts the application and waits for it to end
Parameters: these are parameters sent to the command/program.

When the executed application is a 32-bit GUI application, cmd. EXE returns a command prompt before the application is terminated. If it is executed in the Command Script, the new behavior will not occur.

8. Choice command

Choice uses this command to allow users to enter a single character to run different commands. The/C: parameter should be added for use, and C: should be followed by a prompt to enter characters without spaces. Its return code is 1234 ......

For example: choice/C: dimethyl defrag, mem, end

Will display

Defrag, mem, end [d, M, E]?
Sample:
The content of sample. bat is as follows:
@ Echo off
Choice/C: dimethyl defrag, mem, end
If errorlevel 3 goto defrag (the highest error code should be determined first)
If errorlevel 2 goto mem
If errotlevel 1 goto end

: Defrag
C: \ dos \ defrag
Goto end
: Mem
Mem
Goto end
: End
Echo good bye

After this file is run, defrag, mem, end [d, M, E]? You can select d m e, and then the if statement will make a judgment. D indicates the program segment with the execution label as defrag, and M indicates the program segment with the execution label as mem, E indicates the program segment whose execution label is end. Each program segment finally jumps the program to the end label using goto end. Then, the program displays Good bye, and the file ends.
Echo, @, call, pause, and REM are the most common commands for batch file processing. We started from them. 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 top of other command lines, indicating that the command line itself is not displayed during running.

Call calls another batch file (if another batch file is called directly, subsequent commands of the current file cannot be executed after that file is executed)

Running pause will be paused, and press any key to continue...

Rem indicates that the character after this command is interpreted as a line. If it is not executed, it will only be used for future search.

For example, use Edit to 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: file representation:

Echo off command line not displayed

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 uses WPS annotation to use WPS

Cd ucdos enters the UCDOS directory

WPS use WPS

You can also use parameters in a batch file like the C language, which requires only one parameter identifier %.

% Indicates a parameter. A parameter is a string added after the file name when a batch file is run. Variables can be represented from % 0 to % 9, % 0 indicates the file name itself, and strings are represented in the order of % 1 to % 9.
For example, C: the name of the processing file in the root directory is F. bat, and the content is format % 1.

If c: \> f a: is executed, format:

Another example is C: The name T. bat of the batch of files processed in the root directory, and the content is type % 1 type % 2.

Run c: \> T a.txt B .txt to display the contents of a.txt and B .txt in sequence.

If goto choice for is a relatively advanced command in the batch processing file. If you are familiar with these commands, you are an expert in batch processing files.

If indicates whether the specified conditions are met, and then different commands are executed. There are three formats:

1. If "parameter" = "string" command to be executed

If the parameter is equal to the specified string, the condition is true. Run the command. Otherwise, run the next sentence. (Note that there are two equal signs)

For example, if "% 1" = "A" format:

2. If exist file name command to be executed

If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence. For example, if exist config. sys edit config. sys

3. If errorlevel: command to be executed

If the return code 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 dos returns a number to dos when running the program, which is called the error code errorlevel or return code.

When the Goto batch file runs here, it will jump to the label specified by Goto, which is generally used with if. For example:

Goto end

: End

Echo this is the end

The label is represented by a string. The row where the label is located is not executed.

Choice uses this command to allow users to enter a single character to run different commands. The/C: parameter should be added for use, and C: should be followed by a prompt to enter characters without spaces. Its return code is 1234 ......

For example: choice/CME defrag, mem, end

Will display

Defrag, mem, end [d, M, E]?
For example, the content of test. bat is as follows:

@ Echo off

Choice/CME defrag, mem, end

If errorlevel 3 goto defrag, first judge the highest error code.

If errorlevel 2 goto mem

If errotlevel 1 goto end

EFRAG

C: \ dos \ defrag

Goto end

: Mem

Mem

Goto end

: End

Echo good bye

After this file is run, defrag, mem, end [d, M, E]? You can select d m e, and then the if statement will make a judgment. D indicates the program segment with the execution label as defrag, and M indicates the program segment with the execution label as mem, E indicates the program segment whose execution label is end. Each program segment finally jumps the program to the end label using goto end. Then, the program displays Good bye, and the file ends.

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

Format: For [% F] In (SET) do [command]

If the parameter F is in the specified set, the condition is true and the command is executed.

If one batch file contains one row:

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

It indicates that if a file ends with bat or TXT, the file content is displayed.

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.