[Repost and improve] teach you how to write batch processing step by step
The original author has no professionalism in writing, and the articles he wrote are all kinds of mistakes. Moreover, if it is not modified and improved, it will be a misleading child. Therefore, I modified it based on the original text and corrected most of the errors (of course, there may be new errors, and I hope that the masters can point them out in time after discovering them).
URL: /News/technic/200408/
Excerpted from: World Internet Cafe Alliance Author: Anonymous
Modification and improvement: Climbing(xclimbing@)
Last modified: 19 August 2004
==== willsort Editor's Note =====================================================================
There are many things worth pondering about the following classification of commands. @ in common commands is not a command, but also commonly used commands such as dir copy are not included. All commands in special commands are commonly used for me. It is recommended to divide the commands referenced in batches into three categories: internal commands, external commands, and third-party programs. Another category of internal commands and external commands is that commands dedicated to or commonly used in batches can be called "batch commands".
The following excerpts about "batch commands" in the MS-DOS 6.22 help document. Of course, some of the concepts and definitions are already a little behind.
Batch commands
A batch file or batch program is a body file containing several MS-DOS commands with the extension .BAT. When the name of the batch program is entered at the command prompt, MS-DOS groups execute commands in this batch program.
Any command that is available at the command prompt is available in the batch program. In addition, the following MS-DOS command is specially used in batch programs.
<Call> <If>
<Choice> <Pause>
<Echo> <Rem>
<For> <Shift>
<Goto>
========================================================================
Commonly used commands
echo, @, call, pause, rem (small tips: use :: instead of rem) are the most commonly used commands for batch files, so we start with them.
==== willsort Editor's Note =====================================================================
First of all, @ is not a command, but a special token for DOS batch processing, which is only used to block command line echoes. Here are some special tokens that may be seen in DOS command line or batch processing:
CR(0D) Command line ending character
Escape(1B) ANSI escape character bootstrap
Space(20) Commonly used parameter delimiters
Tab(09); = Uncommonly used parameter delimiter
+ COPY command file connection character
* ?
"" String Delimiter
|
< > >> File redirector
@
/
: Batch Tag Boot
% Batch variable bootstrap
Secondly, :: can indeed play the role of rem annotation, and it is more concise and effective; but there are two points to be paid attention to:
First, except for ::, any character line starting with : is regarded as a label in batch processing, and all subsequent contents are directly ignored. It is only for the purpose of distinguishing it from normal labels. It is recommended to use a label that cannot be recognized by goto, that is, a special symbol followed by a non-alphanumeric number.
Second, unlike rem, the character line after :: will not echo when executed, regardless of whether the command line is opened with echo on or not, because the command interpreter does not consider it a valid command line. From this point of view, rem will be more applicable than :: in some cases; in addition, rem can be used in files.
========================================================================
echo means the character after displaying this command
echo off means that all commands run after this statement do not display the command line itself.
@ is similar to echo off, but it is added at the front of each command line, indicating that the command line that does not display this line at runtime (it can only affect the current line).
call calls another batch file (if you call another batch file directly without calling, you will not be able to return the current file and execute subsequent commands for the current file after executing that batch file).
pause Running this sentence will pause the execution of the batch and display the prompt for Press any key to continue... on the screen, waiting for the user to press any key to continue.
rem means that the characters after this command are interpreted lines (comments) and are not executed, but are only for reference in the future (equivalent to comments in the program).
==== willsort Editor's Note =====================================================================
The description here is more confusing, so it is better to use the command line directly to refer to a command line to help you make it more organized
----------------------------------------------------------------------------
ECHO
When the program is running, the body in the batch program is displayed or hidden. Can also be used to allow or disable echo of commands.
When running a batch program, MS-DOS generally displays (echos) commands in the batch program on the screen.
Use the ECHO command to turn off this feature.
grammar
ECHO [ON|OFF]
To display a command using the echo command, the following syntax is available:
echo [message]
parameter
ON|OFF
Specifies whether to allow echo of the command. To display the current ECHO settings, use ECHO without parameters.
Order.
message
Specifies the body text that allows MS-DOS to display on the screen.
----------------------------------------------------------------------------
CALL
Call another batch program from one batch program without causing an abort of the first batch.
grammar
CALL [drive:][path]filename [batch-parameters]
parameter
[drive:][path]filename
Specifies the name of the batch program to be called and its storage location. The file name must be used as the extension.BAT.
batch-parameters
Specifies the command line information required by the batch program.
----------------------------------------------------------------------------
PAUSE
Pauses the execution of the batch program and displays a message, prompting the user to press any key to continue execution. Only in batches
Use this command in the program.
grammar
PAUSE
----------------------------------------------------------------------------
REM
Add comments to the batch file or You can also use REM commands to block commands (in
You can also use semicolon (;) instead of REM commands in the batch file, but it cannot be replaced).
grammar
REM [string]
parameter
string
Specifies the command to block or the annotation to include.
========================================================================
Example 1: Use edit to edit the file, enter the following content and save it as c:\. After executing the batch file, you can implement it: write all files in the root directory to start UCDOS, enter WPS and other functions.
The content of the batch file is: Command comment:
@echo off does not display subsequent command lines and current command lines
dir c:\*.* > Write the c disk file list to
call c:\ucdos\Call ucdos
echo Hello Show "Hello"
pause pause, wait for the button to continue
rem ready to run wps Comment: ready to run wps
cd ucdos Enter the ucdos directory
wps running wps
Parameters of batch file
Batch files can also use parameters like C functions (equivalent to command line parameters of DOS commands), which requires the use of a parameter identifier "%".
%[1-9] represents a parameter. Parameters refer to a string separated by spaces (or Tabs) after the file name when running a batch file. Variables can range from %0 to %9, %0 represents the batch command itself, and other parameter strings are represented in the order of %1 to %9.
Example 2: C: There is a batch of processing files in the root directory with the following contents:
@echo off
format %1
If C:\>f a:
Then when executing, %1 means a:, so format %1 is equivalent to format a:, so when the above command is run, format a:
Example 3: C: The following batch of processing files in the root directory is:
@echo off
type %1
type %2
Then run C:\>t
%1: means
%2: means
The above command will then display the contents of the file sequentially.
==== willsort Editor's Note =====================================================================
Parameters are also processed as variables in batch processing, so the percentage sign is also used as a bootstrap, followed by a number from 0-9 to form the parameter reference. The relationship between the reference character and the parameter (for example, %1 and a: in the above) is similar to the relationship between the variable pointer and the variable value. When we want to reference the eleventh or more parameters, we must move the parameter start pointer of the DOS. The shift command is acting as the role of this moving pointer, which moves the parameter start pointer to the next parameter, similar to the pointer operation in C language. The illustration is as follows:
Initial state, cmd is the command name, which can be referenced with %0
cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
After 1 shift, cmd will not be cited
cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
After 2 shifts, arg1 is also abandoned, %9 points to empty, and there is no reference meaning.
cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10
^ ^ ^ ^ ^ ^ ^ ^ ^
%0 %1 %2 %3 %4 %5 %6 %7 %8
Unfortunately, shift inverse operation is not supported in win9x and DOS. Only in the nt kernel command line environment, shift supports the /n parameter, and the first parameter can be used as the reference to move the starting pointer.
========================================================================
Previous page12345Next pageRead the full text