A file with the extension name bat (or cmd under nt/2000/xp/2003) is a batch file.
. BAT is a DOS batch file
. cmd is another batch file for NT Kernel command line environments
In a more general sense, Unix shell scripts and other operating systems and even the text that is interpreted by the shell in the application have a very similar effect to the batch file, and the same is done by the dedicated interpreter in the behavior unit, which is more commonly called the scripting language. So from a degree of analysis, batch, Unix shell, awk, Basic, Perl and other scripting languages are the same, but the scope of application and interpretation of the platform are different. Even some applications still use the term batch, and their content and extension are completely different from the DOS batch.
The first 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.
Non-DOS commands can be used in batch files, and even ordinary data files with no executable feature can be used, which is due to the involvement of the new interpretation platform of Windows system, which makes the application of batch processing more and more "marginalized". So the batch process we're talking about should be limited to DOS or command-line environments, otherwise a lot of ideas and settings need to be changed quite a bit.
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.
= = = = = Note ==================
Batch files (batch file) can also be called Batch programs (batch program), which is different from the compiled language, in the case of C, files with extensions C or CPP can be called C language files or C language source code. But only after compiling the attached EXE file can be called C language program. Because the batch file itself has both the readability of the text and the enforceability of the program, the boundaries of these titles are rather vague.
===========================
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.
= = = = = Note =====
For purely DOS systems, executable programs can be subdivided into five categories, sorted by execution priority from high to Low: Doskey macros (pre-resident memory), command. Internal commands in COM (in memory at any time according to the memory environment), the executable program with COM as the extension (loaded by Command.com directly into memory), executable program with EXE-bit extension (loaded in memory by the Command.com after relocation), Batch program with a bat bit extension (interpreted by Command.com, which invokes the first 2,3,4,5 executable program in order of its content, parsing a row, executing a row, and the file itself is not loaded in memory)
============
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
= = = = = Note =====
AUTOEXEC. BAT is an automatic running batch file for DOS system, which is interpreted and executed by Command.com when it starts;
And in the Win9x environment, not only increased support for Dosstart. BAT, Winstart. BAT and many other automatically run batch files, the Autoexec.bat also increased. Dos. W40. Bak. Old. PWS and many other variants to adapt to complex environments and changing needs.
= = = Willsort Note =============
The following classification of orders, there are many worthy of scrutiny. The @ in common commands is not an order, and the commands that are common to dir, copy, and so on are not included, and all commands in special commands are common to me. It is recommended that the commands referenced by the batch be divided into internal commands, external commands, and third party programs. An internal command and an external command have a class of commands that are specific to or commonly used in batches that can be called "batch commands."
The following excerpt from the MS-DOS 6.22 help document on the "batch command" text, of course, some of these concepts and definitions are somewhat behind.
Batch Command
A batch file or batch program is a body file that contains several MS-DOS commands, with the extension. BAT. When you are typing the name of a batch program at a command prompt, MS-DOS executes the commands in this batch program in groups.
Any commands that you can use at the command prompt are available in the batch program. In addition, the following MS-DOS commands are used specifically in a batch program.
==========
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.
= = = = = Note ===========
First, @ is not a command, but a special tag for a DOS batch that is used only for masking command line echoing. The following are some of the special tags that you might see in the DOS command line or batch process:
CR (0D) command line terminator
Escape (1B) ANSI escape character guide
Space (20) commonly used parameter-defining characters
Tab (09); = Non-commonly used parameter-defining characters
+ Copy command file connector
* ? File wildcard characters
"" String definition character
| Command pipe character
< > >> file redirection characters
@ Command line Echo Mask
/Parameter Switch guide
: Batch Label Guide
% Batch Variable guide
Second,:: Can certainly play the role of REM annotation, and more concise and effective; But there are two points to note:
First, in addition to:: Outside, any word that begins with a rows, is regarded as a label in batch processing, and directly ignores all subsequent content, just to distinguish it from normal label, it is suggested to use a label which is not recognized by Goto, that is, a special symbol followed by a non-alphanumeric number.
Second, unlike REM,: the character rows does not echo when it is executed, regardless of whether the command line Echo state is opened with Echo on, because the command interpreter does not consider him to be a valid command line, so rem, in some cases, will be more applicable than: In addition, REM can be used in Config.sys files.
=====================
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).
= = = = = Note =====
The description here is rather confusing, as the command line that directly references a command helps to be more organized
-------------------------
ECHO
Displays or hides the body of the batch program when the program is running. can also be used to allow or disallow echoing of commands.
MS-DOS typically displays (echoing) commands in a batch program on the screen while the batch program is running.
Use the echo command to turn off this feature.
Grammar
ECHO [on| OFF]
To display a command with the echo command, you can use the following syntax:
echo [Message]
Parameters
on| Off
Specifies whether to allow echoing of commands. To display the settings for the current echo, you can use the echo with no parameters
Command.
Message
Specifies the body text to be displayed on the screen by MS-DOS.
-------------------
Call
Invokes another batch program from one batch program without causing the abort of the first batch.
Grammar
Call [Drive:][path]filename [Batch-parameters]
Parameters
[Drive:] [Path]filename
Specifies the name of the batch program to invoke and its location. File name must be used. Bat as an extension.
Batch-parameters
Specifies the command line information required by the batch program.
-------------------------------
PAUSE
Suspends execution of a batch program and displays a message prompting the user to press any key to continue execution. Only in the batch.
This command is used in the procedure.
Grammar
PAUSE
Rem
Add annotations to the batch file or Config.sys. The REM command can also be used to mask commands (in Config.sys
can also be separated by semicolons; Instead of the REM command, but it cannot be overridden in a batch file.
Grammar
REM [string]
Parameters
String
Specifies the command to be masked or the annotation to include.
=======================
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.
= = = = = Note ===============
The parameters are also treated as variables in the batch, so the percent sign is used as a guide, followed by a number in 0-9 to form the parameter reference character. The relationship between a reference character and a parameter, such as%1 and a: above, is similar to the relationship between a variable pointer and a variable value. When we want to refer to the 11th or more arguments, we must move the DOS parameter start pointer. The shift command is acting as the moving pointer, moving the starting pointer of the argument to the next parameter, similar to the pointer operation in C. This is illustrated below:
Initial state, CMD is the command name, you can use%0 Reference
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
%0%1%2%3%5%6%7%8%9
After 1 shift, CMD will not be referenced
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
%0%1%2%3%5%6%7%8%9
After 2 shift, ARG1 is also discarded,%9 point to null, no referential significance
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^
%0%1%2%3%5%6%7%8
Unfortunately, the Win9x and DOS are not supported by the shift reverse operation. The/n parameter is supported only in the NT Kernel command line environment, and can be returned to the start pointer with the first parameter as the Datum.
=================
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:
====
The description for this point in the IF command line help is:
IF [NOT] string1==string2 command
Here are the following points to note:
1. Double quotes that contain strings are not required by syntax, but are only used as a "anti-aircraft" character
2. String1 is not necessarily a parameter, it can also be an environment variable, a loop variable, and other string constants or variables
3. command is not necessary for syntax, and a string2 followed by a space can form a valid command line
=============================
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.
Note ********
You can also use the following usage:
If exist command
Device is a device that has been loaded in a DOS system, usually under Win98:
AUX, PRN, CON, NUL.
COM1, COM2, COM3, COM4
LPT1, LPT2, LPT3, LPT4
XMSXXXX0, EMMXXXX0
A:B: C: ...,
clock$, config$, dblbuff$, ifs$hlp$
Specific content will be slightly different depending on the hard software environment, the use of these device names, you need to ensure that the following three points:
1. The device does exist (except for software virtual devices)
2. The device driver has been loaded (AUX, PRN and other standard devices are defined by the system defaults)
3. The equipment is ready (mainly refers to a:b: ..., com1 ..., lpt1 ...). Wait
Available via command mem/d | Find "Device"/I to review the devices that are loaded in your system
In addition, in the DOS system, the device is also considered a special file, and the file can also be called a character device; Because devices (device) and files are managed using a handle (handle), a handle is a name, similar to a file name, except that the handle is not applied to disk management, but is applied to memory management, which means that device loading is a handle that can be referenced in memory.
==================================
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
= = = = = Note ===========
The return values from large to small are not required, but are only used when the command is Goto, when using set as the execution command, usually from small to large order, such as the need to put the return code into the environment variable, you need to use the following order form:
if errorlevel 1 set el=1
if errorlevel 2 set el=2
if errorlevel 3 set el=3
if errorlevel 4 set el=4
if errorlevel 5 set el=5
...
Of course, the following loops can be used instead, and the principle is consistent:
For%%e in (1 2 3 4 5 6 7 8 ...) do if errorlevel%%e set el=%%e
More efficient and concise usage, you can refer to another article I wrote about getting errorlevel.
This behavior occurs because the if errorlevel comparison of the return code is not equal to, but greater than equal to. Because of Goto jump characteristics, from small to large sort will result in a small return code at the jump out; Because of the "repeat" assignment attribute of the SET command, a large to small sort causes a smaller return code to "overwrite" the larger return code.
In addition, although the IF errorlevel=< number > command is also a valid command line, it is simply command.com to interpret the command line as a command-line delimiter and ignore it
===========================
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.
= = = Willsort Note
Labels are often translated as "tags," but this does not have a broad agreement.
Goto and: Joint use can be implemented midway jump, combined with if can implement the conditional branch of execution process, multiple if can achieve the grouping of commands, similar to the switch case structure in C or the Basic of the Select Case structure, large-scale and structured command grouping function functions in advanced languages can be implemented. The following is a syntax-structured comparison of batch and C/basic:
Batch C/basic
Goto&: Goto&
Goto&:&if if{}&else{}/If&elseif&endif
Goto&:&if ... switch&case/select case
Goto&:&if&set&envar. function ()/function (), sub ()
==================================
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:
CHOICE [/C choices] [/n] [/cs] [/T timeout/d CHOICE] [/M text]
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. "
= = = Willsort Note ===============================
I list win98 under Choice's usage help that has been differentiated
Waits for the user to choose one of a set of choices.
Waits for the user to select a group of selected characters
CHOICE [/c[:]choices] [/n] [/S] [/t[:]c,nn] [text]
/c[:]choices specifies allowable keys. Default is YN
Specifies the allowed keys (the characters to be selected) and defaults to yn
/n Do not display choices and? At end of prompt string.
Do not display the question mark and the selected character in the prompt string
/S Treat Choice keys as case sensitive.
Case sensitive when processing pending characters
/t[:]c,nn Default choice to C after nn seconds
The default choice of C after nn seconds
Text Prompt string to display
The hint string to display
ERRORLEVEL is set to offset the key user presses in choices.
ERRORLEVEL is set to the offset value of the character typed by the user in the selected character
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 errorlevel 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.
current 1/2 page
1 2 Next read the full text