bat file syntax and techniques (batch file or batch program)

2011-04-25  来源:本站原创  分类:OS  人气:120 

bat (batch file or batch program) file syntax and techniques

First of all: the batch file is a text file, each line of the text file is a DOS command

Second: The batch file is a simple procedure, you can use if, goto control flow, for a command to loop

Again: a programmed batch file equivalent to a DOS external command, that is, as long as you bat file storage

Directory set in the path above, then the cmd can be run in any position to the bat file

Finally: C root directory is the first autoexec.bat batch file that runs automatically every time the system starts automatically run the file,

We can often use some software to start writing to the bat file inside, so as not to open the software one by one

The last: the role of a batch, simply means that automatically and continuously execute multiple commands, bat file readable, but also has the enforceability of the program

example: Here is a simple file test.bat

@ Echo off

e:

dir

D: \ java_world \ eclipse-jee-europa-fall-win32 \ eclipse \ eclipse.exe

pause

The bat to do is: First switch to the E drive, then drive under the file limit E, and finally start the eclipse, the last stop in the cmd interface

bat file commonly used commands:

1 echo: that shows the character of this command

2 echo off: means that this statement does not show all the commands to run the command line itself

3 @: This command is similar with the echo off, that does not appear when running the command line (the impact of the current line)

4 call: calling another batch file (if you do not call, then the execution of another bat file would not return to the current file after the execution oh)

5 pause: run to the sentence, it will pause the cmd window and display press any key to continue ... Press any key to continue after the command

6 rem: comments, do not perform, just to refer to himself in the future (oh techniques: use:: instead of wailing rem)

bat file parameters

Batch files can use the same parameters as the C language (the equivalent of dos commands command-line parameters), it takes a parameter identifier%

Parameter can be 1 to% 9% expressed (Note:% 0 bat file itself), multiple parameters separated by space or tab

example:

Suppose F disk: test.bat reads as follows

@ Echo off

type% 1

type% 2

Then, enter cmd and switch to the F drive to run F: \> text a.txt b.txt

Then passed to% 1 b.txt a.txt will be passed to% 2, is the role of the command sequence displays the contents of a.txt and b.txt

bat file the special command

if, goto, choice, for the senior, oh, if you are proficient, well, I'll call you expert

One, if conditional statement is used to determine compliance with the provisions of three formats:

1, if [not] "parameters" == "string" to be the command (Note: the command to be executed in our oh)

Meaning: parameter is equal to [not equal to (not that range)] specify a string, set up to run the command, or running the next command

2, if [not] exist [path \] filename command to be executed (Note: the command to be executed in our oh)

example: if exist c: \ config.sys type c: \ config.sys

Meaning: If the c drive there config.sys, then display its contents

3, if errorlevel <number> command to be executed

End of the run many DOS programs will return a numeric value used to indicate the results of the program is running (or state), if errorlevel command can be judged by the return value of the program, depending on the return value to decide to implement different commands (the return value must be according to descending order). If the value equals the number specified, then the condition is true, run the command, or run the next one.

example: if errorlevel 2 goto x2

Two, goto

goto to jump to specified label (label with a colon: followed by the standard string to define) goto statement is generally used in conjunction with the if, under different conditions to perform a different command set

example: goto end

: End (Note: The label format ": string", label the line not be executed Oh)

echo this is the end

Three, choice

choice to use this command allows the user to enter a character (for selection), which returns the user to choose a different errorlevel, then on if errorlevel with, according to the user's choice to run different commands.

Note: choice command for DOS or Windows system to provide external commands, different versions of the command syntax are slightly different choice, please use the choice /? View usage

choice of the command syntax (the syntax of choice for Windows 2003 in command of the syntax, the other version of the choice of the command syntax similar to this):

CHOICE [/ C choices] [/ N] [/ CS] [/ T timeout / D choice] [/ M text]

Description:

The tool allows the user to select an item from the selection list and return the selected index.

Parameter list:

/ C choices to create a list of options specified. The default list is "YN".

/ N at the prompt to hide list of options. Tips to get in front of the news shows that option is still enabled.

/ CS allows the selection of case-sensitive option. By default, this tool is not case sensitive.

/ T timeout before making the default choice, the number of seconds pause. Acceptable values ​​are from 0 to 9999. If you specify 0, there will be no pause, the default option

Will be selected.

/ D choice nnnn seconds specified in the default option. Character must use the / C option specifies a set of choices; the same time, we must use / T specified nnnn.

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

/? Displays help message.

Note:

ERRORLEVEL environment variable is set to select the set of selected key from the index. The first option listed returns 1, the second choice to return 2, and so on. If the user presses a key that is not a valid option, the tool will be issued a warning sound. If the tool detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses Ctrl + Break or Ctrl + C key, the tool will return an ERRORLEVEL value of 0. In a batch process use the ERRORLEVEL parameter, the parameter in descending order.

Example:

CHOICE /?

CHOICE / C YNC / M "to confirm, press Y, no press N, or cancel, press C."

CHOICE / T 10 / C ync / CS / D y

CHOICE / C ab / M "Option 1 Please choose a, option 2 choose b."

CHOICE / C ab / N / M "Option 1 Please choose a, option 2 choose b."

If I run the command: CHOICE / C YNC / M "to confirm, press Y, no press N, or cancel, press C."

Displayed on the screen:

Confirm press Y, and not press N, or cancel, press C. [Y, N, C]?

Example: test.bat reads as follows (note that with the return value to determine if errorlevel, according to the return value from highest to lowest):

@ 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

echo good bye

This batch is running, it will display "defrag, mem, end [D, M, E]?", The user can choose dme, then the if statement to make judgments based on the user's choice, d that the implementation of the program segment labeled defrag, m that the implementation of the program segment labeled mem, e that the implementation of the label for the end of the block, each block in order to eventually jump to the program goto end label at the end, then the program will display good bye, the end of the batch run

Four, for loop command, as long as conditions are met, it will repeatedly execute a unified command

Syntax:

Of a group of files in each file to execute a specific command.

FOR%% variable IN (set) DO command [command-parameters]

%% Variable Specifies a single letter replaceable parameter.

(Set) Specifies one or a group of files. You can use wildcards.

command Specifies the command for each file.

command-parameters Specifies parameters for a specific command or command line switch.

For example, a batch file has a line:

for%% c in (*. bat *. txt) do type%% c

The command line will show current directory to bat and txt file for the extension of the content.

To bat a few examples below:

1. IF-EXIST create a file named TEST3.BAT, as follows:

@ Echo off

IF "% 1" == "A" ECHO XIAO

IF "% 2" == "B" ECHO TIAN

IF "% 3" == "C" ECHO XIN

If you are running:

C: \> TEST3 ABC

Displayed on the screen:

XIAO

TIAN

XIN

If you are running:

C: \> TEST3 AB

Will be displayed on the screen

XIAO

TIAN

During the execution of this command, DOS will be assigned to an empty string parameters% 3

2, IF-ERRORLEVEL

Establish TEST4.BAT, as follows:

@ ECHO OFF

XCOPY C: \ AUTOEXEC.BAT D: IF ERRORLEVEL 1 ECHO file copy failed

IF ERRORLEVEL 0 ECHO successfully copy files

Then execute the file:

C: \> TEST4

If the file is copied successfully, the screen will display "Success copy files", otherwise it will display "File copy failed."

IF ERRORLEVEL is used to test it on a DOS command return values, attention is only on the return value of a command and return value must be in descending order according to the order of judge. Therefore, the following batch file is wrong:

@ ECHO OFF

XCOPY C: \ AUTOEXEC.BAT D: \

IF ERRORLEVEL 0 ECHO successfully copy files

IF ERRORLEVEL 1 ECHO copy files not found

IF ERRORLEVEL 2 ECHO users ctrl-c copy operation aborted

IF ERRORLEVEL 3 ECHO preset error prevents the file copy operations

IF ERRORLEVEL 4 ECHO write disk errors during the copy

Whether copies of the success behind:

Copy file not found

Users ctrl-c copy operation aborted

Preset error prevents the file copy operations

Write disk errors during the copy

Will be displayed.

Here are a few commonly used commands and their representatives, the return value of the significance of:

backup

0 Backup succeeded

A backup file not found

2 to complete the backup file-sharing to prevent conflict

3 users to use ctrl-c stop backup

4 Since the fatal error that the backup operation aborted

diskcomp

0 same as

A comparison of different

2 users ctrl-c stop comparison

3 fatal errors due to comparison operation aborted

4 preset error abort more

diskcopy

0 copy operation was successful

1 non-fatal disk read / wrong

2 users ctrl-c copy operation ends

3 due to a fatal stop error handling to make disk copy

4 preset error block copy operations

format

0 format success

3 users ctrl-c stop formatting

4 due to a fatal error to format the suspension of processing

5 At the prompt "proceed with format (y / n)?" The end of the user type n

xcopy

0 files successfully copied

1 copy of the file not found

2 users ctrl-c copy operation aborted

4 preset error prevents the file copy operations

5 write disk errors during the copy

3, IF STRING1 == STRING2

Establish TEST5.BAT, document reads as follows:

@ Echo off

IF "% 1" == "A" formAT A:

Execution:

C: \> TEST5 A

Appeared on the screen, whether the A: drive formatted content.

Note: In order to prevent parameter is empty, usually the string with double quotes (or other symbol, be careful not to use a reserved symbol) in quotes.

Such as: if [% 1] == [A], or if% 1 *== A *

5, GOTO

Establish TEST6.BAT, document reads 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 an ASCII character colon ":" colon and no spaces between the labels.

(2) label the naming and naming the same file name.

(3) DOS supports up to eight characters of the label, when no difference between the two labels, it will jump to the most recent label.

6, FOR

The establishment of C: \ TEST7.BAT, document reads as follows:

@ ECHO OFF

FOR%% C IN (*. BAT *. TXT *. SYS) DO TYPE%% C

Run:

C:> TEST7

Executed, the screen will be C: root directory of all the BAT, TXT, SYS extension of the file contents are displayed (not including hidden files)

Transfer: http://blog.csdn.net/shibenjie/archive/2009/06/07/4249817.aspx

相关文章
  • bat file syntax and techniques (batch file or batch program) 2011-04-25

    bat (batch file or batch program) file syntax and techniques First of all: the batch file is a text file, each line of the text file is a DOS command Second: The batch file is a simple procedure, you can use if, goto control flow, for a command to lo

  • BAT file syntax and techniques (bat file preparation and use) 2010-11-11

    BAT file syntax and techniques (bat file preparation and use) (reprint) More useful to more useful things the East first, the batch file is a text file, each line of this file is a DOS command (most of the time as if we in the DOS prompt, execute the

  • BAT file syntax and techniques 2011-01-14

    BAT file syntax and techniques (bat file preparation and use) First, the batch file is a text file, each line of this file is a DOS command (most of the time as if we in the DOS prompt, execute the same command line), you can use the Edit under DOS o

  • BAT batch file and folder (zz) 2011-04-06

    1 built bat file to automatically execute copying, delete commands. Example 1: The following is a copy cd.dll files to windows \ system32 bat file contents: copy cd.dll% windir% \ system32 Example 2: The following line is to uninstall windows \ syste

  • oracle execute SQL statements using the batch file, bat oracle database connection and execute the statement 2010-11-02

    First create a text file, written on the inside Sql Code sqlplus scott / tiger @ [email protected] Then save the file, the file extension to. Bat Then again and again a similar directory, create a text file Written on the inside Sql Code - To have t

  • oracle using a batch file to execute SQL statements, bat to connect oracle database and execute statements 2010-11-02

    First create a text file, written on inside Sql Code sqlplus scott / tiger @ [email protected] Then save the file, the file extension to. Bat Then more points at the same level directory, create a text file Written on the inside Sql Code - The sys a

  • Windows batch file operation 2011-02-26

    1.copy-- file copy command will copy one or more files to another location. The so-called copy the original file is not changed, re-created a content and document any differences between the original file. COPY [/ D] [/ V] [/ N] [/ Y | /-Y] [/ Z] [/

  • Batch file to delete svn files and clas 2010-05-01

    package com.delete.dir; import java.io.File; /** * First, compile the class file :javac com\delete\dir\Delete.java * Then run the file : *java com.delete.dir.Delete C:\\Users\\sjs\\Desktop\\projectName * Or write a batch file to delete .bat, The cont

  • Oracle backup and recovery simple batch file - change 2010-03-31

    Recommendation one: Using Task Scheduler, batch files, and the EXP ORACLE export feature, can date back up files automatically generated ORACLE, ORACLE greatly facilitates data backup. : 1, the establishment of a batch file backup.bat \. exp system /

  • oracle service batch file 2010-03-15

    Recently in the Alley Oracle, start closing day service too much trouble, remembering the long ago of the batch file Although there is a lot of people would, or write about it Start: @ECHO OFF ECHO Starting OracleService, please wait ... net start Or

  • Ant compile the VS2005 program call the batch file 2010-06-14

    1, ant call the batch file, compiled with VS2005 MSBuild process (1) set the environment variable Path increase in D: \ Program Files \ Microsoft Visual Studio 8 \ VC \ vcpackages; C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727; (Note: MSBuild

  • Use the batch file to execute the sql 2010-06-23

    REM ********** REM Modify the values below to match your configuration REM ********** set v_mysqlHost=192.168.*.130 set v_mysqlPort=3306 set v_mysqlUser=admin set v_mysqlPassword=111111 set v_mysqlDatabase= testBase REM if the mysql sever deployed on

  • How to write a batch file to automatically import data into oracle database 2010-10-02

    Oracle Database automatically import and export, in many places there is a wide range of applications. For example: an Oracle database application system development after the end of the system if you want to install to the client computer, you need

  • How to use a batch file to a program as a system service since the launch of each boot 2010-11-05

    How to use a batch file to a program as a system service since the launch of each boot Sc command not only with Command line using the sc command. Detailed on the sc command, your own view Help (sc /?), Simply mentioned how this added to the system s

  • Get the batch file path 2010-12-08

    @echo off echo The batch file : echo The full path :%~dp0 echo The file name is :%~n0.bat pause >nul

  • java batch file name change 2010-12-26

    Today in the file name changes loom on the volume, always enclosed in parentheses, are accurate. I am so excited that he wrote the two classes to realize, the fight had a batch file can run directly run the run.bat.

  • Batch file to start off WebSphere Service 2011-05-12

    1) Create a batch file off WebSphere, WebSphereStop.bat file. Contents of the file format is as follows: @ Net stop "IBMWAS61Service - fe7b4feb60834adNode01" @ Exit Note: WebSphere red font for the name of the service to be shut down, the servic

  • Get a batch file path 2010-12-08

    @echo off echo The batch file : echo The full path where :%~dp0 echo File name is :%~n0.bat pause >nul

  • How to write a batch file to automatically import the data into oracle database 2010-10-02

    Oracle database automatically import and export, in many places there is a wide range of applications, such as: development of an Oracle database application system after the installation if you want the system to the client computer, you need to ini

  • Oracle data import and export imp / exp? Batch file 2011-07-04

    How to write a batch file to automatically import the data into oracle database Oracle Database automatically import and export, in many places there is a wide range of applications, for example: an Oracle database application development end, if you