Method for making bat files.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

echo, @, call, pause, REM (tip::: instead of REM) is the most commonly used batch fileCommand, We began to learn from them.
Echo to show theCommandAfter the character
Echo off says all run in this statementCommandDo not displayCommandFor itself
@Like echo off, but it is added to eachCommandThe front line, said the runtime does not display this lineCommandRow (can only affect the current line).
Call calls another batch file (if not call and direct calls to other batch file, then execute the batch file will be unable to return the current file and the execution of the current document follow-upCommand).
Pause to run this sentence suspended for batch execution and displayed on the screen of Press any key to continue... Tips, waits for the user to press any key to continue
REM said theCommandAfter the character to explain the line (notes), do not perform, just give yourself for future reference (equivalent to in the notes program).

Example 1: Editor A. editbatFile, input the content after the disk is C:/a.bat, Executing the batch file can realize: the root directory of all documents written in a.txt, start UCDOS, enter the WPS function.

  The batch file content:         CommandNotes:

    @Echo off does not show the followingCommandAnd the currentCommandThat's ok
    dir c:/*.* >A.txt will be written to the a.txt settings file list
    call c:/ucdos/ucdos.bat    Call UCDOS
    Hello echo display " hello"
    Pause paused, waiting for the key to continue
    REM prepared to run the WPS note: ready to run WPS
    CD UCDOS into the UCDOS directory.
    WPS running WPS  

Parameters of a batch file

The batch file can also use the parameters such as the C language function as (equivalent to DOSCommandTheCommandLine parameters), the need to use a parameter identifier"%".

%[1-9]Said parameters, parameter refers to run batch add at the end of the file name processing file to the empty (or Tab) separated string. Variables can be from%0 to%9,%0 said the batchCommandItself, other parameter string to%9 sequence with%1.

Patients with 2:C: under the root directory is a batch file named F.bat, Content:
@echo off
format %1

If the implementation of C:/>f a:
So in the implementation of F.batWhen a:,%1 said, so format%1 is equivalent to format a:, then the aboveCommandThe actual execution runtime is format a:

Patients with 3:C: under the root directory of a batch file named t.bat, Content:
@echo off
type %1
type %2

Then run the C:/>t a.txt b.txt
%1: a.txt
%2: b.txt
So the aboveCommandThe sequence showed that a.txt and the contents of the b.txt file.


If goto choice for is a senior in batch fileCommand, If you use this several very skilled, expert you are batch file.

One, if is a statement, to judge whether it meets the prescribed conditions, which determines the implementation of differentCommand. There are three kinds of type :

1, if [not] "The parameter " = = " " string; stay of executionCommand

If the parameters (not is equal to the said range, the same below) a specified string, then the condition is established, operationCommand, Otherwise, run the next sentence.

An example: if "%1"=="a" format a:

2, if [not] Exist [/] file path name to be executedCommand
If the specified file, then the condition is established, operationCommand, Otherwise, run the next sentence.

Such as: if exist C:/config.sys type c:/config.sys
If there are c:/config.sys files, displays its contents.

3, if errorlevel <Digital > stay of executionCommand

Many DOS programs at the end of the run will return a digital value used to indicate the result of running the program (or state), by if errorlevelCommandCan determine the program returns value, according to the different return value to determine the execution of differentCommand(return value must be arranged according to the sequence from big to small). Returns value if the; equal to the specified number, then the condition is established, operationCommand, Otherwise, run the next sentence.

As of if errorlevel 2 goto x2

Two, the goto batch file to run here will jump to the goto specified by the label (label label, label: followed by the standard string to define), the goto statement is generally used in conjunction with the if, according to different conditions to perform differentCommandGroup.

Such as:

goto end

echo this is the end

The label ": string" to define, label where the line is not performed.

The use of three, choiceCommandAllows the user to input a character (for choice), and different errorlevel according to the user's choice to return, and then in the if errorlevel, according to the user's choice of running differentCommand.

Be careful: choiceCommandExternal DOS or Windows systemCommand, Different versions of choiceCommandThe syntax is slightly different, please use choice / view usage?.

ChoiceCommandGrammar (the syntax for the choice Windows 2003CommandGrammar, other versions of choiceCommandGrammar and the similar):

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

The tool allows the user to select an item from the list of choices and returns the selected item index.

The parameter list:
/Cchoices specifies a list of options to create. The default list is "YN".

/N to hide the list of options in prompt. In front of the news shows that,
The option is enabled.

/CS allows the selection of small write options. By default, this tool
Is case insensitive.

/Before Ttimeout made the default selection, a few seconds pause. Acceptable value from 0
To 9999. If you specify 0, will not be suspended, the default option
Will be selected.

/Dchoice specifies the default option in nnnn seconds. Characters must be in /C
A specified set of choices; at the same time, must be specified with the nnnn /T.

/Mtext specifies the prompt to display before the news. If not specified, the tool only
Display prompts.

/?Display a help message.

Be careful:
The ERRORLEVEL environment variable is set as the key index set selection from the selected. The first selected list
Choose to return 1, second choose to return 2, etc.. If the user presses the button is not a valid choice,
The tool will issue a warning sound. If the tool detects errors, it will return 255
ERRORLEVEL value. If the user presses the Ctrl+ Break or Ctrl+ C bond, the tool will return 0
ERRORLEVEL value. Use the ERRORLEVEL parameter in a number of applications, the parameter reduction

CHOICE /C YNC /M "Confirm please press Y, or press N, or press C to cancel. "
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Please select the a option 1, option 2 select b. "
CHOICE /C ab /N /M "Please select the a option 1, option 2 select b. "

If I runCommand: CHOICE /C YNC /M "Confirm please press Y, or press N, or press C to cancel. "
The screen will display:
Confirm please press Y, or press N, or press C to cancel. [Y,N,C]?

An example: test.batThe contents are as follows. (note, if errorlevel returned value judgment; when, according to the return value ranging from high to low):
@echo off
choice /C dme /M "defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto mem
if errotlevel 1 goto defrag

goto end

goto end

echo good bye

This batch operation, will show "defrag, MEM, end[D, M, E]?", the user can choose d m e, then the if statement according to the user's choice of judge, said D executive labeling program segment defrag, m says executive labeling program segment MEM, e says executive labeling program section end, each segment end to goto end the program jumps to the end label, then the application will display the good bye, the end of a batch run.

Four, the for cycleCommand, As long as they meet the requirements, it will be executed multiple times with the sameCommand.

For a set of files for each file performs a specificCommand.

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

%%Variable specifies the parameters of a single letter substitution.
(set)Specify one or a set of files. You can use wildcards.
Command is specified for each file executionCommand.
For a specificCommandThe specified parameter orCommandLine switch.

For example, a batch file in a row:
for %%c in (*.bat *.txt) do type %%c

TheCommandWill display the current directory of all thebatAnd TXT is the extension of the file content.

Batch sample



First, use Notepad in C:/ to build a test1.batThe batch file, the file contents are as follows:
@echo off

And then run it:

If C:/ is AUTOEXEC.BATThe file, its contents will be displayed, if it does not exist, the batch will prompt you for the file does not exist.


Then build a test2.batFile, as follows:
IF NOT EXIST /%1 ECHO /%1 does not exist

TheCommandThe operation results.

(1) IF EXIST is used to test if the file exists, formula
IF EXIST [the path to the + file name]; Command
(2) test2.batFile%1 is a parameter, DOS allows the transfer of 9 batch of parameter information for batch files, respectively%1~%9 (%0 test2CommandItself), which are a little like the programming of the arguments and formal parameters,%1 parameters, AUTOEXEC.BATIs argument.

3) Further, the establishment of a named TEST3.BATFile, as follows:
@echo off
IF "%1" == "A" ECHO XIAO
IF "%2" == "B" ECHO TIAN
IF "%3" == "C" ECHO XIN

If you are running:
The screen will display:

If you are running:
The screen will display

In thisCommandIn the process of implementation, the DOS will be an empty string specified for the parameter%3.


The establishment of TEST4.BAT, The contents are as follows:
IF ERRORLEVEL 0 ECHO to copy files

Then the executable file:

If the file copy success, screen will display "successful copy files", otherwise it will display "file copy failure".

IF ERRORLEVEL is used to test it in the last DOSCommandReturns value, pay attention to only the lastCommandReturns value, and returns value must be in accordance with the judge from big to small order sequence.
So the batch file below is wrong:
IF ERRORLEVEL 0 ECHO to copy files
IF ERRORLEVEL 1 ECHO did not find a copy of the file
IF ERRORLEVEL 2 ECHO ctrl-c users to suspend the copy operation
IF ERRORLEVEL 3 ECHO initialization error block file copy operation
Disk error writing the IF ERRORLEVEL 4 ECHO the copying process

Regardless of the copy is successful, the back:

Copy file not found
The user through the ctrl-c to abort the copy operation
Preset error prevented a file copy operation
Disk error writing the copying process

Will be displayed.

Here are several commonly usedCommandReturns value and its meaning:
0 backup successfully
1 not found backup file
2 file sharing conflict to stop the backup is complete
3 user ctrl-c backup aborted
4 because of a fatal mistake the backup operation aborted

0 compared to the same
1 Comparison of different
2 ctrl-c users to suspend the comparison operation
3 because the fatal mistake make the comparison operation aborted
4 preset error abort comparison

0 disk copy operation is successful
1 non fatal disk read / write
2 ctrl-c users end copy operation
3 for the treatment of fatal error so that the disk copy aborted
4 preset error prevented the copy operation

0 Type of success
3 user abort through ctrl-c type processing;
4 for the treatment of fatal error type of suspension
5 in the "proceed with format (y/n prompt)?" the end user to type n

0 successful copy files
The 1 copy file not found
2 ctrl-c users stop copy operation
4 preset error prevented the file copy operation
Disk error writing 5 copying process


The establishment of TEST5.BAT, The contents of the file are as follows:
@echo off
IF "%1" == "A" formAT A:

Appeared on the screen will A: disk type of content.

Note: in order to prevent parameter is null, the general will string in double quotation marks (or other symbols, note that you cannot use the reserved symbol) in parentheses.
Such as: if [%1]==[A] or if%1*==A*


The establishment of TEST6.BAT, The contents of the file are as follows:

Be careful:
(1) Label is ASCII characters in ": " colon;, not between colon and label free.
(2) The same naming rules for naming rules and file label name.
(3) DOS supports a maximum of eight character label, when unable to distinguish between two label, will jump to a recent label.

6, FOR

The establishment of C:/TEST7.BAT, The contents of the file are as follows:


After implementation, the screen will be under the root directory of all the C:BAT, TXT, SYS is the extension of the file content is displayed (not including hidden files.

The original address

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jennie at January 04, 2014 - 8:09 AM