BAT file syntax

xiaoxiao2021-03-06  41

First, the batch file is a text file. Every line of this file is a DOS command (most of us seem to be the same as the command line executed by the DOS prompt), you can use the Edit or Windows under DOS. Any text file editing tool such as Notepad is created and modified.

Second, the batch file is a simple program that can control the process running through the Conditional Statement (IF) and Process Control Statement (GOTO), and can also use the loop statement (for) to perform a command in the batch. . Of course, the programming capabilities of batch files are very limited, and it is very unregulated. Batch program statement is a DOS command (including internal commands and external commands), and the capacity of batch depends primarily on the command you are using.

Third, each prepared batch file is equivalent to a DOS external command, you can put it in your DOS search path (PATH) to make it run in any location. A good habit is to create a BAT or BATCH directory (for example, C: / Batch) on the hard disk, then put all your written batch files in this directory, so you just set up C: / Batch in PATH, you You can run all your written batch programs in any location.

Fourth, under the DOS and WIN9X / ME system, the autoexec.bat batch file under the root directory is automatically run batch file. You can run the file each time the system is started, you can start the system each time The command to run is placed in this file, such as setting the search path, tune the mouse driver and disk cache, set system environment variables. Below is an example of autoexec.bat running under Windows 98:

@Echo off

Path C: / Windows; C: / UCDOS; C: / Dostools; C: / SyStools; C: / Wintools; C: / Batch

LH smartdrv.exe / x

Lh doskey.com / insert

LH CTMouse.exe

SET TEMP = D: / TEMP

SET TMP = D: / TEMP

Batch treatment

Simply put, the role of batch is the automatic continuous implementation of multiple commands.

Here first, a simplest application: When starting the WPS software, you must execute each time (> the previous content represents the DOS prompt):

C: /> CD WPS

C: / WPS> SPDOS

C: / WPS> PY

C: / WPS> WBX

C: / WPS> WPS

If you do this before every time you use WPS, do you feel very trouble?

Ok, use batch processing, you can simplify these troubles, first we write a runwps.bat batch file, the content is as follows:

@echo off

C:

CD / WPS

SPDOS

py

WBX

WPS

CD /

In the future, we have access to WPS each time, just run the running of RunWPs.

Common command

Echo, @, call, pause, remote: Use :: Replace REM) is the most commonly used commands of the batch file, and we start learning from them.

Echo indicates the character after displaying this command.

Echo off means that all running commands after this statement does not display the command line itself

@ @ And the echo offline, but it is the most in front of each command line, indicating that the command line does not show this line when running (only the current line).

Call calls another batch file (if you do not use Call to directly call other batch files, then the current file will not be returned after the batch file is executed and the current file will be executed. PAUSE runs this sentence to suspend the execution of the apparatus and display the prompts of the press any key to contact ... on the screen, wait for the user to continue after pressing any key

REM indicates that the characters after this command are interpreted (notes), not executed, just give yourself reference (equivalent to comments in the program).

Example 1: Edit a.bat file with Edit, enter the following contents After the store is c: /a.bat, you can implement it after executing the batch file: write all the files in the root directory to A.TXT, start UCDOS, enter WPS and other functions.

The content of the batch file is: Command Note:

@echo off does not show the subsequent command line and the current command line

Dir c: /*.*> a.txt writes the C disk file list to A.txt

Call C: /ucdos/ucdos.bat call UCDOS

echo Hello, "Hello"

Pause pause, wait for the button to continue

REM is ready to run WPS Note: Prepare to run WPS

CD UCDOS enters UCDOS directory

WPS runs WPS

Batch file parameters

Batch files can also use parameters like the C language function (equivalent to the command line parameters of the DOS command), which requires a parameter representator "%".

% [1-9] indicates parameters, and parameters refer to a string separated by space (or Tab) after running the batch file. Variables can indicate the batch command itself from% 0 to% 9,% 0, and other parameter strings are represented by% 1 to% 9.

Example 2: C: There is a batch of processing files in the root directory name F.BAT, the content is:

@echo off

Format% 1

If you do C: /> f A:

Then when F.Bat is executed,% 1 represents A:, so FORMAT% 1 is equivalent to format A:, then the above command is actually executed in Format A:

Example 3: C: The next batch of processing files in the root directory name is T.BAT, the content is:

@echo off

TYPE% 1

TYPE% 2

So running c: /> t a.txt b.txt

% 1: Represents A.txt

% 2: Represents B.TXT

The above command will sequentially display the contents of the A.TXT and B.TXT files.

Special command

If Goto Choice for is a relatively advanced command in a batch file. If you are very skilled, you are an expert in batch files.

First, if the IF is a conditional statement, it is used to determine whether it meets the specified conditions, thereby deciding to perform different commands. There are three formats:

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

If the parameter is equal to (NOT means not et al, the following) The specified string, the condition is established, run the command, otherwise run the next sentence.

Example: if "% 1" == "a" format A:

2, if [not] exist [path /] file name to be executed

If you have a specified file, the condition is true, run the command, otherwise run the next sentence.

Such as: if exist c: /config.sys type c: /config.sys

Indicates that it is displayed if there is a C: /Config.sys file. 3, if Errorlevel The command to be executed

Many DOS programs return a numeric value after running, it is used to indicate the result (or status) of the program run (or status), and the return value of the program can be determined by the if ErrorLevel command, determine the execution of different commands according to different return values ​​(return value must Arranged from large to small order). If the return value is equal to the specified number, the condition is established, run the command, otherwise run the next sentence.

If Errorlevel 2 Goto X2

Second, the GOTO batch file is running here will jump to the reference numeral specified by Goto (label, label is used: After the standard string is defined), the GOTO statement is generally used in conjunction with the IF, depending on the different conditions. Command group.

Such as:

Goto end

: END: END

echo this is the end

The label is defined with the ": string", and the routing is not executed.

Third, Choice uses this command to allow users to enter a character (for choice) to return different ErrorLevel according to the user's selection, and then run different commands according to the user's selection.

Note: The choice command is the external command provided by the DOS or Windows system. Different versions of the Choice command speech will be slightly different. Please use the khoice /? View.

Choice's command syntax (this syntax is the syntax of the choice command in Windows 2003, and the other version of the Choice command syntax is similar to this.):

Choice [/ cchoices] [/ n] [/ cs] [/ t timeout / d choice] [/ m text]

description:

This tool allows users to select an item from the selection list and return the index of the selected item.

parameter list:

/ C Choices Specifies the list of options to create. The default list is "YN".

/ N hides the list of options in the prompt. Tip the previous message get displayed,

The option is still in an enabled state.

/ Cs allows selection options to select. This tool is by default

Is not case-sensitive.

/ T timeout makes the number of seconds before the default selection. Acceptable values ​​are from 0

To 9999. If 0 is specified, there will be no pause, the default option

Will get the choice.

/ D Choice Specifies the default option after NNNN seconds. Character must be selected with / c

A set of options specified in the item; at the same time, NNNN must be specified with / t.

/ M text Specifies the message to display before prompt. If not specified, the tool is only

Show prompts.

/? Display help messages.

note:

The ErrorLevel environment variable is set to the key index selected from the selection set. The first choice listed

Alternative to 1, the second selection returns 2, and so on. If the user presses the key is not a valid choice,

This tool will make a warning sound. If the tool detects an error status, it will return 255

Errorlevel value. If the user presses Ctrl Break or CTRL C key, the tool will return 0

ERRORLEVEL value. When using an Errorlevel parameter in a batch, the parameters drop

Order.

Example:

CHOICE /?

Choice / C YNC / M "Confirm, press Y, no Please press N, or cancel, press C."

CHOICE / T 10 / C YNC / CS / D Y

Choice / C Ab / M option 1 Select a, option 2 Please select B. "Choice / C Ab / N / M" option 1 Select a, Option 2 Please select B. "

If I run the command: choice / c ync / m "Confirm, press Y, no, please press N, or cancel, press C."

The screen will be displayed:

Confirm, press Y, no, press N, or cancel, press C. [Y, n, c]?

Example: Test.bat is as follows (note that when the return value is used to determine the return value with if ErrorLevel, press the return value from high to low alignment):

@echo off

Choice / C DME / M "DEFRAG, MEM, END"

IF Errorlevel 3 Goto end

IF Errorlevel 2 Goto Mem

IF Errotlevel 1 goto defrag

: defrag

C: / DOS / DEFRAG

Goto end

: MEM

MEM

Goto end

: END: END

Echo good bye

After this batch run, "DEFRAG, MEM, END [D, E]?", The user can select DME, then the IF statement determines based on the user's selection, D represents the block, M, M, M, M, M Indicates a block that executes the label MEM, and e means executing the program of the label End, and each block is finally jumped to the END Number with goto End, and then the program will display good Bye, the batch run.

Fourth, for loop command, as long as the condition is in line with, it will execute the same command multiple times.

grammar:

Perform a particular command to each file in a set of files.

For %% Variable In (SET) Do Command [Command-Parameters]

%% variable specifies a single letter replaceable parameter.

(SET) Specify one or a set of files. You can use wildcards.

Command specifies the command to execute each file.

Command-parameters

Specify a parameter or command line switch for a specific command.

For example, there is a line in a batch file:

For %% c in (* .bat * .txt) Do Type %% C

The command line displays all the contents of the files with BAT and TXT-based files in the current directory.

Batch example

IF-EXIST

1)

First use the notepad in C: / create a TEST1.BAT batch file, the contents of the file are as follows:

@echo off

If exist /autoexec.bat type /autoexec.bat

If not exist /autoexec.bat echo /autoexec.bat does not exist

Then run it:

C: /> Test1.bat

If C: / exists autoeexec.bat file, then its content will be displayed, if there is no existence, the batch will prompt you if the file does not exist.

2)

Then establish a Test2.bat file, the content is as follows:

@Echo off

IF EXIST /% 1 TYPE /% 1

IF not exist /% 1 ECHO /% 1 does not exist

carried out:

C: /> Test2 AutoExec.bat

This command runs the result.

Description:

(1) If exist is used to test whether the file is existing, the format is

IF exist [Path File Name] command

(2)% 1 in the test2.bat file is the parameter, the DOS allows 9 batch parameter information to batch file, which is% 1 ~% 9 (% 0 means the Test2 command itself), this is a bit like programming The relationship between the parameters,% 1 is the form, and Autoexec.Bat is inform. 3) Further, create a file called Test3.bat, the content is as follows:

@echo off

IF "% 1" == "a" echo xiao

IF "% 2" == "b" echo tian

IF "% 3" == "c" echo xin

If you run:

C: /> TEST3 A b C

The screen will be displayed:

Xiao

Tian

Xin

If you run:

C: /> TEST3 a B

Display on the screen

Xiao

Tian

During this command execution, DOS specifies an empty string to the parameter% 3.

2, if-errorlevel

Establish TEST4.BAT, the content is as follows:

@Echo off

Xcopy C: /autoexec.bat D: IF Errorlevel 1 Echo file copy failed

IF Errorlevel 0 Echo success copy file

Then execute the file:

C: /> TEST4

If the file copy is successful, the screen will display "Successful Copy File", otherwise "file copy failed" will be displayed.

If ErrorLevel is used to test the return value of its previous DOS command, pay attention to the return value of the last command, and the return value must be determined in order from large to small order.

So the batch file below is wrong:

@Echo off

Xcopy C: /autoexec.bat D: /

IF Errorlevel 0 Echo success copy file

IF Errorlevel 1 Echo Did not find a copy file

If Errorlevel 2 Echo users suspended copy operation via Ctrl-C

IF Errorlevel 3 Echo Preset Error Block File Copy Operation

IF Errorlevel 4 Echo copy process during writing

Whether the copy is successful, followed by:

No copy file is found

User via CTRL-C suspension

Preset error Block file copy operation

Write disk error during copying

Will be displayed.

The following is the return value of several common commands and their representatives:

BACKUP

0 backup success

1 Did you find a backup file

2 File shared conflict blocks backup completion

3 users use Ctrl-C to stop backup

4 Make backup operations to abort the backup operation due to fatal errors

Diskcomp

0 disk is more

1 disk comparison

2 users suspend comparison operation via Ctrl-C

3 Since the fatal error makes the comparison operation

4 Preset error abort

Diskcopy

0 disks copy operation

1 non-pendant read / write wrong

2 Users end copy operation via Ctrl-C

3 Copy stop due to fatal processing errors

4 Preset error block copy operation

Format

0 Successfully

3 users suspend formatted processing via CTRL-C

4 Surperse formatting due to fatal processing errors

5 Type N of N under the prompt "Proceed with format (y / n)?"

xcopy

0 Success copy file

1 Did you find a copy file

2 users suspend copy operation via CTRL-C

4 Preset error Block file copy operation

5 write disk errors during copying

3, if string1 == String2

Establish TEST5.BAT, the file content is as follows:

@echo off

IF "% 1" == "a" Format A:

carried out:

C: /> TEST5 A

Whether the A: disc is formatted on the screen.

Note: In order to prevent the parameters from being empty, the string will generally use double quotes (or other symbols, notice that the reserved symbol) is enclosed.

Such as: IF [% 1] == [a] or if% 1 * == a *

5, goto

Establishing Test6.bat, the document content is as follows:

@Echo off

IF exist c: /autoexec.bat goto _copy

Goto _done

: _COPY

Copy C: /autoexec.bat D: /

: _DONE

note:

(1) Before the label is the colon of the ASCII character ":", there is no space between the colon and the label.

(2) Name rules of the label are the same as the name rules of the file name.

(3) DOS supports the label of the longest eight characters. When it is not possible to distinguish two labels, it will jump to the nearest label.

6, for

Establish C: / Test7.bat, the document content is as follows:

@Echo off

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

run:

C:> Test7

After execution, all the C: Panhe TXT, SYS is displayed in BAT, TXT, and SYS (excluding the hidden file).

转载请注明原文地址:https://www.9cbs.com/read-70790.html

New Post(0)