windows batch commands related to document preparation

2011-07-20  来源:本站原创  分类:OS  人气:83 

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

Here to talk about the one of the most simple application: wps software at startup, must be performed each time (> in front of content that DOS prompt):
C: \> cd wps
C: \ WPS> spdos
C: \ WPS> py
C: \ WPS> wbx
C: \ WPS> wps
If you use WPS for each execution again before all this, you do not think a lot of trouble?

Well, with a batch, you can simplify the operation of these problems, we first write a runwps.bat batch file, as follows:
@ Echo off
cd \ wps
cd \

Later, every time we enter the wps, only need to run runwps this batch file.

Commonly used commands

echo, @, call, pause, rem (tip: use:: instead of rem) is the most common of several batch file command, we start learning from them since.
echo command that shows the character of this
echo off that this statement does not show all the commands to run the command line itself
@ Echo off much the same with, but it is added to the top of each command line, that line is not displayed when running the command line (only affects the current line).
call call another batch file (if you do not call directly call another batch file, then executing a batch file that will not return the current file and the subsequent execution of the current file command).
pause will pause the batch run execution of sentence and displayed on the screen Press any key to continue ... prompt, waiting for the user to press any key to continue
rem that the character of this command to explain the line (note), does not perform, but to their own future reference (equivalent to a program note).

Example 1: edit edit a.bat file, type the following and save them to c: \ a.bat, execute the batch file can be achieved: to write all the files in the root directory a.txt, start UCDOS, enter WPS and other functions.

Batch file contents: Remarks:

@ Echo off command line does not show up and the current command line
dir c: \ *.*> a.txt file list will be written a.txt c drive
call c: \ ucdos \ ucdos.bat call ucdos
echo hello display "hello"
pause pause and wait for key to continue
rem ready to run wps Note: wps ready to run
cd ucdos into ucdos directory
wps wps run

Batch file parameters

Batch files can also be the same as C language function parameter (equivalent to DOS command command-line parameters), which need to use a parameter that character "%."

% [1-9] indicates that the parameter, the parameter is when running the batch file after the file name added to a space (or Tab) delimited string. Variable from% 0 to% 9% 0 of the batch itself, other parameters string with% 1 to% 9 the order said.

Example 2: C: root directory with a batch file named f.bat, says:
@ Echo off
format% 1

If you do C: \> fa:
So when running f.bat,% 1 says that a:, this format% 1 is equivalent to format a:, then run the above command to actually execute the format a:

Example 3: C: root directory of the next batch file named t.bat, says:
@ Echo off
type% 1
type% 2

Then run C: \> t a.txt b.txt
% 1: said a.txt
% 2: indicates b.txt
So the above command will sequentially display the contents of the file a.txt and b.txt.

Special order

if goto choice for the more advanced batch file commands, if you use these very skilled, you're a batch file expert friends.

One, if the conditional statement is used to determine whether the required conditions, and thus decided to implement a different command. There are three formats:

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

If the parameter is equal to (not that range, the same below) the specified string, the condition is established, run command, or run the next one.

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

2, if [not] exist [path \] filename command to be executed if the specified file, then the condition is true, run the command, or run the next one.

Such as: if exist c: \ config.sys type c: \ config.sys
That if there is c: \ config.sys file, 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.

As if errorlevel 2 goto x2

Two, goto run the batch file here to jump to goto the specified label (the label that is label, label with a: followed by a string to standard definition) at, goto statement is generally used in conjunction with the if, under different conditions to perform different command group.

Such as:

goto end

: End
echo this is the end

Label use ": string" to define, label the line is not executed.

Three, 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]

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 show,
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
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.

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 choice,
The tool will be issued a warning sound. If the tool detects an error condition, it will return 255
ERRORLEVEL value. If the user presses Ctrl + Break or Ctrl + C key, the tool will return 0
The ERRORLEVEL value. In a batch process use the ERRORLEVEL parameter, the parameter in descending order.

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
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 the same command.

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.
Specify 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.

Batch Examples



First, use Notepad in C: \ a test1.bat batch file, as follows:
@ Echo off

Then run it:

If the C: \ AUTOEXEC.BAT file exists, then its contents will be displayed, if not, it will prompt you that the batch file does not exist.


Then re-establish a test2.bat file, as follows:
IF EXIST \% 1 TYPE \% 1
IF NOT EXIST \% 1 ECHO \% 1 does not exist

The result of the command above.

(1) IF EXIST is used to test whether a file exists, the format
IF EXIST [path + file name]
(2) test2.bat file% 1 is the parameter, DOS allows information to pass nine batches to the batch file parameters, respectively,% 1% 9 (% 0 test2 command itself), this is a bit like a real programming and formal parameters of the relationship between% 1 parameter, AUTOEXEC.BAT is the argument.

3) Further, 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:
Displayed on the screen:

If you are running:
C: \> TEST3 AB
Will be displayed on the screen

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


Establish TEST4.BAT, as follows:
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:
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:

Users to copy files not found ctrl-c stop error prevented the file copy operations copy the preset operation 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:
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

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

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

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

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


Establish TEST5.BAT, document reads as follows:
@ Echo off
IF "% 1" == "A" FORMAT A:

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 *


Establish TEST6.BAT, document reads as follows:

(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:
FOR%% C IN (*. BAT *. TXT *. SYS) DO TYPE%% C


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).


  • windows batch commands related to document preparation 2011-07-20

    Simply put, the role of a batch, the batch is the role of automatic continuous execute multiple commands. Here to talk about the one of the most simple application: wps software at startup, must be performed each time (> in front of content that DOS

  • Windows Phone Commands 2012-03-30

    Windows Phone Commands 网站 : 这是一个基于 Microsoft .NET 4.0 Framework 的开源项目,能够作为独立存储的资源管理器以及部署应用来辅助Windows Phone 7.1的开发. Connect Screen Launch Application/Settings Explorer Phone Isolate Storage Editor Manifest File Depl

  • Windows Batch commonly used commands 2011-02-26

    1.ECHO echo command @ Character on the command before closing the command echo, echo time regardless of whether the open. The role of the echo command are listed below: (1) Open the echo off Echo Format: echo [{on | off}] echo off the role of the equ

  • Restart the batch commands windows explorer 2010-11-19

    @ Echo off taskkill / f / im explorer.exe start explorer.exe exit

  • Windows Batch common sense 2011-02-24

    The nature of the batch is a bunch of DOS command a certain order to form a collection. Batch, has become a batch script, English as a BATCH, batch file suffix. Bat or. Cmd. In a batch file, each line can be regarded as a command, each command can co

  • Windows Batch common symbols 2011-02-25

    1 @ echo command line Shield symbol ECHO OFF command can close off the entire batch of echo, but can not turn off the ECHO OFF command itself, and now we ECHO OFF command in front of a @, we can not echo the command to all the requirements. The role

  • 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] [/

  • Document preparation tools were upgraded 2010-04-06

    Main features: Before the upgrade the system, First of all, this upgrade will be needed to cover the file, copy the directory structure in accordance with the original came out, back in the designated areas. If upgrade problems, you can use to restor

  • net user commands related to 2010-12-11

    Keywords: net user command Note: The following commands support only the user administrator rights, ordinary users can not use these commands! 1, increase user net user userName password / add (Note: The slash must be preceded by a space) 2, upgrade

  • DTD document preparation 2010-04-29

    1, XML description of the DOCTYPE <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""> <!DOCTYPE beans SYSTEM ""&g

  • windows batch study notes 2010-11-17

    In the C: \ Documents and Settings \ meiyoudao> following the establishment of a file in the file input test.bat dir ping Open a cmd window (input test Enter, you can run the command) C: \ Documents and Settings \ meiyoudao> test Enter in

  • windows batch delete files 2011-02-23

    Need to batch delete some files, if the dos command, the volume can be removed quickly. Click the "Start" → "Run", type: "CMD" after the carriage return. Then in the "Command Prompt" window, enter: del F: \ _desktop

  • Linux commands related to user and group (MEMO) 2010-09-07

    useradd - create a new user or update default new user information. groupadd - create a new group users - print the user names of users currently logged in to the current host 'groups - print the groups a user is in id - print real and effective user

  • oracle sql commands related 2010-09-24

    The default installation of Oracle Oracle, the beginner should be aware of some of the SQL statement 1 connection SQL * Plus system / manager 2, shows the currently connected users SQL> show user 3, view the system with which users SQL> select * fro

  • [Links] TOMCAT5 under the WINDOWS system services related to the command line 2010-09-30

    Tomcat5.exe Tomcat can join the service; Tomcat5w.exe can assist configuration has been added services. Tomcat5w.exe monitoring program Tomcat5w is a monitoring and configuring Tomcat services, graphical interface applications. Available command line

  • Windows batch directory operations 2011-02-26

    1.CD-- Displays the current directory name, or change the current directory Displays the current directory name, or to change the current directory. CHDIR [/ D] [drive:] [path] CHDIR [..] CD [/ D] [drive:] [path] CD [..] .. Changed to specify the par

  • Tomcat under Linux commands related to 2011-03-29

    Start tomcat: ./ Close tomcat: ./ See tomcat log: tail -n200 logs/catalina.out View tomcat process: ps -ef | grep tomcat Close tomcat process: kill -9 The process of ID

  • windows shortcut commands commonly used 2011-07-04

    Open the Add or Remove Programs appwiz.cpl start calc calculator conf start netmeeting devmgmt.msc Device Manager dxdiag-check information Open explorer explorer gpedit.msc-group policy System Configuration Utility msconfig mspaint drawing board msts

  • Standardized project development document preparation 2011-09-05

    In the project development process, should be required to write a good thirteen kinds of documents, with specific documentation requirements, accuracy, clarity, integrity, flexibility and traceability.

  • Use Hudson's project to build 2009-12-10

    Translated: First, build a common project Hudson can be used to perform specific work of building a server, such as continuous / formal / night build, run tests, or perform repeatab