First, the batch file is a text file, and each line of the file is a DOS command (most of the time like the command line we execute at the DOS prompt), you can use the DOS edit or Windows Notepad (Notepad) Any text file Editing tool creates and modifies a batch file.
Second, batch files are simple programs that control the flow of commands by conditional statements (if) and Process Control statements (goto), and loop statements (for) can be used to loop through a command in batch processing. Of course, the programming ability of batch files and C language programming statements is very limited, but also very nonstandard. The batch process statement is a DOS command (including internal commands and external commands), and the ability to batch processing depends largely on the commands you use.
Third, each prepared batch file is equivalent to a DOS command, and you can place it in your DOS search path (path) so that it can run anywhere. A good habit is to create a bat or BATCH directory on your hard disk (for example, C:\BATCH) and then put all the batch files you write into that directory so that you can run all of your batch programs in any location if you set up C:\batch on the path.
Four, under DOS and win9x/me systems, C: The Autoexec.bat batch file in the packing directory is the automatic running batch file. This file is automatically run every time the system starts, and you can put commands that you want to run every time the system starts up, such as setting the search path, driving the mouse and disk caching, setting the system environment variables, and so on. The following is an example of a Autoexec.bat running under Windows 98:
@ECHO off
PATH C:\WINDOWS; C:\WINDOWS\COMMAND; 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
The role of batch processing
Simply put, the effect of batch processing is to automatically execute multiple commands consecutively.
Here is the simplest application: When you start the WPS software, you must perform each time (> The preceding content represents a DOS prompt):
C:\>CD WPS
C:\wps>spdos
C:\wps>py
C:\wps>wbx
C:\wps>wps
Do you find it troublesome to do this once every time you use WPS?
Well, with the batch process, you can simplify these troublesome operations, first we write a runwps.bat batch file, which reads as follows:
@echo off
C:
Cd\wps
Spdos
Py
Wbx
Wps
cd\
Later, each time we enter WPS, only need to run runwps this batch file.
Common commands
Echo, @, call, pause, REM (tip:: Instead of REM) are some of the most commonly used commands for batch files, and we start with them.
Echo indicates the character after this command is displayed
echo off means that none of the commands running after this statement show the command line itself
@ is the same as echo off, but it is added to the front of each command line, indicating that the runtime does not display the command line for this line (only the current row is affected).
Call invokes another batch file (if you call other batch files directly without calling), you cannot return the current file and execute subsequent commands for the current file after the batch file is executed.
Pause running this sentence suspends the execution of the batch and displays the press any key to continue on the screen ... Prompts, waits for the user to press any key to continue
REM represents the word identifier the line (note) after the command, not executing, but only for future reference (equivalent to a comment in the program).
Example 1: Edit the A.bat file with edit, enter the following content for C:\a.bat, after the execution of the batch file can be implemented: All files in the root directory into the a.txt, start Ucdos, into WPS and other functions.
The contents of the batch file are: Command comment:
@echo off does not show subsequent command lines and the current command line
Dir c:\*.* >a.txt writes a list of C-disk files to A.txt
Call C:\ucdos\ucdos.bat calls Ucdos
echo, how do you show "Hello"?
Pause paused, waiting for key to continue
REM Ready to run WPS Note: Ready to run WPS
CD Ucdos into Ucdos directory
WPS Operation WPS
Parameters for batch Files
Batch files can also use parameters (equivalent to the command line arguments of DOS commands) as functions in C, which requires a parameter notation "%".
%[1-9] Represents a parameter, which is a string separated by a space (or tab) that is added after the file name when the batch file is run. Variables can represent the batch command itself from%0 to%9,%0, and other parameter strings are represented in the order of%1 to%9.
Example 2:C: The root directory has a batch of processing file name F.bat, the content is:
@echo off
Format%1
If you execute c:\>f a:
When the F.bat is executed,%1 represents a:, so that the format%1 is equivalent to format a: The above command actually executes format a when it is run:
Example 3: C: Root directory The next batch of processing file name is T.bat, the content is:
@echo off
Type%1
Type%2
Then run c:\>t a.txt b.txt
%1: Indicates a.txt
%2: Indicates b.txt
The commands above will then display the contents of the A.txt and B.txt files sequentially.
Special commands
If Goto choice for is a more advanced command in a batch file, you are an expert in batch files if you use them skillfully.
An if is a conditional statement that is used to determine whether a specified condition is met, thus determining the execution of a different command. There are three different formats:
1, if [not] "parameter" = = "string" command to be executed
If the argument is equal to the string specified by the (not as unequal), the condition is set, the command is run, or the next sentence is run.
Example: If "%1" = = "A" format a:
2, if [not] exist [path \] FileName The command to be executed
If you have the specified file, the condition is set, run the command, or run the next sentence.
Such as: if exist C:\Config.sys type C:\Config.sys
Indicates that if a C:\Config.sys file exists, its contents are displayed.
3, if errorlevel < numbers > pending orders
Many DOS programs return a numeric value to indicate the result (or state) of the program's operation at the end of the run, using the IF ERRORLEVEL command to determine the return value of the program, and to perform different commands depending on the return value (the return value must be in order from large to small). If the return value equals the specified number, the condition is set, the command is run, or the next sentence is run.
if errorlevel 2 goto x2
Goto batch file Run here will jump to goto the specified label (marking is label, marking with: followed by a standard string to define), goto statements are generally used in conjunction with if, according to different conditions to execute different command groups.
Such as:
Goto END
: End
The echo this are the end
The label is defined by ": string", and the line of the label is not executed.
Choice Use this command to allow the user to enter a character (for selection), which returns a different errorlevel depending on the user's choice, and then to the if errorlevel to run a different command depending on the user's choice.
Note: The choice command has a slightly different version of the choice command syntax for the external commands provided by DOS or Windows systems, please use choice/? To view the usage.
The choice command syntax, which is the syntax for the Choice command in Windows 2003, is similar to the command syntax for other versions of choice:
Describe:
The tool allows the user to select an item from the select 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. Prompts the previous message to be displayed,
option is still enabled.
/cs allows you to select options that are case-insensitive. By default, this tool
is written in the same case.
/T Timeout The number of seconds to pause before making a default selection. The acceptable value is from 0
to 9999. If 0 is specified, there is no pause, default option
Will get a choice.
/d choice Specifies the default option after nnnn seconds. Characters must be selected in/C
In a set of selections specified by the At the same time, nnnn must be specified with/T.
/M text Specifies the message to display before prompting. If not specified, the tool only
Displays a hint.
/? Displays a help message.
Attention:
The ERRORLEVEL environment variable is set to the key index selected from the selection set. The first selection listed
Choose to return 1, the second option to return 2, and so on. If the user presses a key that is not a valid selection,
The tool emits a warning sound. If the tool detects an error state, it returns a 255
ERRORLEVEL value. If the user presses the Ctrl+break or CTRL + C key, the tool returns 0
the ERRORLEVEL value. When using the ERRORLEVEL parameter in a batch program, the parameter is dropped
Ordinal arrangement.
Example:
CHOICE/?
CHOICE/C ync/m "Confirm please press Y, no please press N, or cancel please press C." "
choice/t 10/c ync/cs/d y
CHOICE/C ab/m Option 1 Please select a, option 2, select B. "
CHOICE/C ab/n/M "option 1 Please select a, option 2, select B. "
If I run the command: choice/c ync/m "confirm, press Y, please press N, or cancel please press C." "
The screen will show:
Confirm Press Y, please press N, or cancel, press C. [Y,n,c]?
For example: The contents of the Test.bat are as follows (note that the return value is ordered from high to low by the IF errorlevel):
@echo off
CHOICE/C dme/m "Defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto MEM
If Errotlevel 1 goto defrag
:d Efrag
C:\dos\defrag
Goto END
: Mem
Mem
Goto END
: End
echo Good bye
After this batch is run, the "Defrag,mem,end[d,m,e]" is displayed, the user can select D M E, and the IF statement is judged according to the user's choice, d indicates that the program segment is executed as Defrag, and M represents the program segment where the label is mem. E is the program segment that executes the label end, and each program segment ends with Goto end, and then the program displays the good bye, and the batch run ends.
The For loop command, which executes the same command multiple times, as long as the condition is met.
Grammar:
Executes a specific command on each file in a set of files.
For%%variable in (set) do command [Command-parameters]
%%variable specifies a single letter replaceable parameter.
(set) to specify one or a set of files. You can use wildcard characters.
command specifies the commands that are executed for each file.
Command-parameters
Specify a parameter or command-line switch for a specific command.
For example, there is one row in a batch file:
For%%c in (*.bat *.txt) do type%%c
The command line displays the contents of all files with bat and txt extensions under the current directory.
Batch processing sample
1. If-exist
1)
First use Notepad to create a test1.bat batch file in C:\, which reads as follows:
@echo off
IF EXIST \autoexec. BAT TYPE \autoexec. BAT
IF not EXIST \autoexec. BAT ECHO \autoexec. BAT does not exist
And then run it:
C:\>test1. BAT
If a Autoexec.bat file exists in the C:\, its contents are displayed, and if not, the batch prompts you that the file does not exist.
2)
Then a test2.bat file is created, which reads as follows:
@ECHO off
IF EXIST \%1 TYPE \%1
IF not EXIST \%1 ECHO \%1 does not EXIST
Perform:
C:\>test2 AUTOEXEC. BAT
The command runs the same result as above.
Description
(1) If EXIST is used to test the existence of the file, the format is
IF EXIST [path + filename] Command
(2) The%1 in the Test2.bat file is a parameter and DOS allows 9 batches of parameter information to be passed to the batch file,%1~%9 (% 0 for the test2 command itself), which is somewhat like the relationship between actual and formal parameters in programming,%1 is a formal parameter, AUTOEXEC. BAT is an argument.
3 Further, establish a document called Test3.bat, which reads 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 A B C
The screen will show:
XIAO
TIAN
Xin
If you are running:
C:\>test3 A B
The screen will show
XIAO
TIAN
During the execution of this command, DOS assigns an empty string to parameter% 3.
2, If-errorlevel
The establishment of the Test4.bat, the contents are as follows:
@ECHO off
XCOPY C:\AUTOEXEC. BAT d:if errorlevel 1 ECHO file copy failed
IF ERRORLEVEL 0 ECHO successfully copies files
Then execute the file:
C:\>test4
If the copy of the file is successful, the screen will display "Copy files successfully", or "file copy Failed" will be displayed.
IF errorlevel is used to test the return value of its last DOS command, note that only the return value of the previous command is returned, and the return value must be judged in order from large to small.
The following batch file is therefore incorrect:
@ECHO off
XCOPY C:\AUTOEXEC. BAT D:\
IF ERRORLEVEL 0 ECHO successfully copies files
IF ERRORLEVEL 1 ECHO does not find the copy file
IF ERRORLEVEL 2 ECHO user aborts copy operation via Ctrl-c
IF ERRORLEVEL 3 ECHO preset error prevents file copy operation
Write disk error in the IF errorlevel 4 ECHO copy process
Whether or not the copy succeeds, the following:
Copy file not found
User aborts copy operation via Ctrl-c
Preset errors prevent file copy operations
Write disk error in copy process
will show up.
The following are the return values of several commonly used commands and the meaning of their representations:
Backup
0 Backup Successful
1 No backup files found
2 File share conflicts prevent backup from completing
3 User aborts backup with ctrl-c
4 Abort the backup operation due to fatal error
Diskcomp
0 disks are the same
1-Disk comparison different
2 user aborted comparison operation via Ctrl-c
3 The comparison operation aborted due to fatal error
4 Preset Error Abort comparison
diskcopy
0 Disk copy operation succeeded
1 non-fatal disk read/write error
2 user end copy operation via Ctrl-c
3 The disk copy is aborted due to fatal processing error
4 Preset error block copy operation
Format
0 format Success
3 user aborted format processing via CTRL-C
4 formatting aborted due to fatal handling error
5 at the prompt "Proceed with format (y/n)?" Next user type N end
Xcopy
0 successful copying of files
1 No Copy files found
2 user aborted copy operation via Ctrl-c
4 Preset error prevents file copy operation
5 Write disk error in copy process
3, IF STRING1 = = STRING2
Establish Test5.bat, the contents of the document are as follows:
@echo off
IF "%1" = = "A" FormAT A:
Perform:
C:\>test5 A
The screen displays the contents of the format a: disk.
Note: In order to prevent the argument from being empty, you will typically enclose the string in double quotes (or other symbols, note that you cannot use a reserved symbol).
such as: if [%1]==[a] or if%1*==a*
5, GOTO
Establish Test6.bat, the contents of the document are as follows:
@ECHO off
IF EXIST C:\AUTOEXEC. BAT GOTO _copy
GOTO _done
: _copy
COPY C:\AUTOEXEC. BAT D:\
: _done
Attention:
(1) Before the label is the ASCII character of the colon ":", the colon and the label can not have spaces between.
(2) The naming rule for the label is the same as the naming rule for the filename.
(3) DOS supports a maximum of eight-digit characters, and when two labels cannot be distinguished, jumps to the nearest label.
6, for
Establish C:\TEST7. BAT, the contents of the file are as follows:
@ECHO off
For%%c in (*. BAT *. TXT *. SYS) do TYPE%%c
Run:
C:>test7
After execution, the screen will display all the contents of the file (excluding hidden files) with the bat, TXT, and SYS extensions in the C: Packing directory.
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.