### Detailed Explanation of DOS Batch Processing Commands I. Introduction to Simple Batch Processing Internal Commands
1. Echo Command
Turn on or off the request echo function, or display a message. If there are no parameters, the echo command will display the current echo setting.
Syntax
echo
Sample: @echo off / echo hello world
In practical applications, we will combine this command with the redirection symbol (also called the pipe symbol, generally using > >> ^) to implement inputting some commands into files in a specific format. This will be reflected in subsequent examples.
2. @ Command
Indicates not to display the command after @. In the process of intrusion (such as using a batch to format the enemy's hard disk), of course, you cannot let the other party see the commands you use.
Sample: @echo off
@echo Now initializing the program,please wait a minite...
@format X: /q/u/autoset (The format command cannot use the /y parameter. Fortunately, Microsoft has left the autoset parameter for us, and the effect is the same as /y.)
3. Goto Command
Specify to jump to a label. After finding the label, the program will process the commands starting from the next line.
Syntax: goto label (label is a parameter, specifying the line in the batch program to which it is to be transferred.)
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms (If you don't understand if, %1, %2 here, skip it first, and there will be detailed explanations later.)
@Rem check parameters if null show usage
:noparms
echo Usage: monitor.bat ServerIP PortNumber
goto end
The name of the label can be chosen arbitrarily, but it is better to have meaningful letters. A colon is added before the letter to indicate that this letter is a label. The goto command finds where to jump next according to this colon. It is best to have some explanations so that others can understand your intention.
4. Rem Command
Comment command, which is equivalent to /*--------*/ in C language. It will not be executed, but only plays a role of comment, which is convenient for others to read and for your own future modification.
Rem Message
Sample: @Rem Here is the description.
5. Pause Command
When the Pause command is run, the following message will be displayed:
Press any key to continue . . .
Sample:
@echo off
:begin
copy a:*.* d:\back
echo Please put a new disk into driver A
pause
goto begin
In this example, all files on the disk in drive A are copied to d:\back. When the displayed comment prompts you to put another disk into drive A, the pause command will suspend the program so that you can change the disk, and then press any key to continue processing.
6. Call Command
Call another batch program from one batch program, and the parent batch program is not terminated. The call command accepts the label used as the call target. If used outside a script or batch file, Call will not work on the command line.
Syntax
call FileName ] ]
Parameters
FileName
Specify the location and name of the batch program to be called. The filename parameter must have the .bat or .cmd extension.
7. start Command
Call an external program. All DOS commands and command-line programs can be called by the start command.
Common parameters for intrusion:
MIN Start the window minimized
SEPARATE Start a 16-bit Windows program in a separate space
HIGH Start the application in the HIGH priority category
REALTIME Start the application in the REALTIME priority category
WAIT Start the application and wait for it to end
parameters These are parameters passed to the command/program
When the executed application is a 32-bit GUI application, CMD.EXE returns to the command prompt without waiting for the application to terminate. If it is executed in a command script, this new behavior will not occur.
8. choice Command
The choice command allows the user to enter a character, thereby running different commands. When using it, the /c: parameter should be added. The characters that can be entered should be written after c:, with no spaces in between. Its return code is 1234......
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end?
Sample:
The content of Sample.bat is as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag (Should judge the highest numerical error code first)
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
After this file is run, it will display defrag,mem,end? The user can choose d m e, and then the if statement will make a judgment. d means executing the program segment marked defrag, m means executing the program segment marked mem, e means executing the program segment marked end. Each program segment finally uses goto end to jump the program to the end label, and then the program will display good bye, and the file ends.
9. If Command
if means to judge whether the specified condition is met, so as to decide to execute different commands. There are three formats:
1. if "parameter" == "string" command to be executed
If the parameter is equal to the specified string, the condition is true, run the command, otherwise run the next sentence. (Note that there are two equal signs)
For example, if "%1"=="a" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms
2. if exist filename command to be executed
If the specified file exists, the condition is true, run the command, otherwise run the next sentence.
For example, if exist config.sys edit config.sys
3. if errorlevel / if not errorlevel number command to be executed
If the return code is equal to the specified number, the condition is true, run the command, otherwise run the next sentence.
For example, if errorlevel 2 goto x2
All DOS programs return a number to DOS when running, called the error code errorlevel or return code. The common return codes are 0 and 1.
10. for Command
The for command is a relatively complex command, mainly used to execute commands in a loop within a specified range of parameters.
When using the FOR command in a batch file, use %%variable to specify the variable.
for {%variable│%%variable} in (set) do command
%variable specifies a single letter replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specify parameters or command-line switches for a specific command.
When using the FOR command in a batch file, use %%variable to specify the variable instead of %variable. The variable name is case-sensitive, so %i is different from %I
If command extensions are enabled, the following additional FOR command formats will be supported:
FOR /D %variable IN (set) DO command
If wildcards are included in the set, it means matching directory names instead of file names.
FOR /R path] %variable IN (set) DO command
path, and point to the FOR statement in each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.) character, enumerate the directory tree.
FOR /L %variable IN (start,step,end) DO command
%variable IN (file-set) DO command
FOR /F %variable IN ("string"
DO command
FOR /F %variable IN ('command'
DO command
Or, if the usebackq option is available:
FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ("string"
DO command
FOR /F %variable IN ('command'
DO command
filenameset is one or more file names. Before continuing to the next file in filenameset, each file has been opened, read, and processed.
Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more symbols. Then the for loop is called with the symbol string variable value found. By default, /F separates through the first blank symbol in each line of each file. Skip blank lines. You can replace the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:
eol=c - refers to the end of a line comment character (just one)
skip=n - refers to the number of lines to be ignored at the beginning of the file.
delims=xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs.
tokens=x,y,m-n - refers to which symbols of each line are passed to each iteration of for itself. This will lead to the format of additional variable names as a range. Specify the last character star of the m symbol string through the nth symbol,
then the additional variables will be allocated and accept the reserved text of the line after the last symbol parsing.
usebackq - specifies that the new syntax has been used in the following situations:
When executing a backquoted string as a command and the quote character is a literal string command and allows the use of double quotes to enclose file names in fi.
sample1:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do command
It will analyze each line in myfile.txt, ignore those lines starting with a semicolon, and pass the second and third symbols in each line to the for program body; delimited by commas and/or spaces. Please note that the statement of this for program body refers to %i to get the second symbol, refers to %j to get the third symbol, and refers to %k to get all the remaining symbols after the third symbol. For file names with spaces, you need to enclose the file name with double quotes. In order to use double quotes in this way, you also need to use the usebackq option, otherwise, the double quotes will be understood as being used to define a string to be analyzed.
%i is specifically explained in the for statement, and %j and %k are specifically explained through the tokens= option. You can specify up to 26 symbols in a line through tokens=, as long as you do not try to explain a variable higher than the letter 'z' or 'Z'. Please remember that FOR variables are single letters, case-sensitive, and global; at the same time, there cannot be more than 52 in use.
You can also use the FOR /F analysis logic on adjacent strings; the method is to enclose the filenameset between parentheses with single quotes. In this way, the string will be regarded as a single input line in a file.
1. Echo Command
Turn on or off the request echo function, or display a message. If there are no parameters, the echo command will display the current echo setting.
Syntax
echo
Sample: @echo off / echo hello world
In practical applications, we will combine this command with the redirection symbol (also called the pipe symbol, generally using > >> ^) to implement inputting some commands into files in a specific format. This will be reflected in subsequent examples.
2. @ Command
Indicates not to display the command after @. In the process of intrusion (such as using a batch to format the enemy's hard disk), of course, you cannot let the other party see the commands you use.
Sample: @echo off
@echo Now initializing the program,please wait a minite...
@format X: /q/u/autoset (The format command cannot use the /y parameter. Fortunately, Microsoft has left the autoset parameter for us, and the effect is the same as /y.)
3. Goto Command
Specify to jump to a label. After finding the label, the program will process the commands starting from the next line.
Syntax: goto label (label is a parameter, specifying the line in the batch program to which it is to be transferred.)
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms (If you don't understand if, %1, %2 here, skip it first, and there will be detailed explanations later.)
@Rem check parameters if null show usage
:noparms
echo Usage: monitor.bat ServerIP PortNumber
goto end
The name of the label can be chosen arbitrarily, but it is better to have meaningful letters. A colon is added before the letter to indicate that this letter is a label. The goto command finds where to jump next according to this colon. It is best to have some explanations so that others can understand your intention.
4. Rem Command
Comment command, which is equivalent to /*--------*/ in C language. It will not be executed, but only plays a role of comment, which is convenient for others to read and for your own future modification.
Rem Message
Sample: @Rem Here is the description.
5. Pause Command
When the Pause command is run, the following message will be displayed:
Press any key to continue . . .
Sample:
@echo off
:begin
copy a:*.* d:\back
echo Please put a new disk into driver A
pause
goto begin
In this example, all files on the disk in drive A are copied to d:\back. When the displayed comment prompts you to put another disk into drive A, the pause command will suspend the program so that you can change the disk, and then press any key to continue processing.
6. Call Command
Call another batch program from one batch program, and the parent batch program is not terminated. The call command accepts the label used as the call target. If used outside a script or batch file, Call will not work on the command line.
Syntax
call FileName ] ]
Parameters
FileName
Specify the location and name of the batch program to be called. The filename parameter must have the .bat or .cmd extension.
7. start Command
Call an external program. All DOS commands and command-line programs can be called by the start command.
Common parameters for intrusion:
MIN Start the window minimized
SEPARATE Start a 16-bit Windows program in a separate space
HIGH Start the application in the HIGH priority category
REALTIME Start the application in the REALTIME priority category
WAIT Start the application and wait for it to end
parameters These are parameters passed to the command/program
When the executed application is a 32-bit GUI application, CMD.EXE returns to the command prompt without waiting for the application to terminate. If it is executed in a command script, this new behavior will not occur.
8. choice Command
The choice command allows the user to enter a character, thereby running different commands. When using it, the /c: parameter should be added. The characters that can be entered should be written after c:, with no spaces in between. Its return code is 1234......
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end?
Sample:
The content of Sample.bat is as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag (Should judge the highest numerical error code first)
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
After this file is run, it will display defrag,mem,end? The user can choose d m e, and then the if statement will make a judgment. d means executing the program segment marked defrag, m means executing the program segment marked mem, e means executing the program segment marked end. Each program segment finally uses goto end to jump the program to the end label, and then the program will display good bye, and the file ends.
9. If Command
if means to judge whether the specified condition is met, so as to decide to execute different commands. There are three formats:
1. if "parameter" == "string" command to be executed
If the parameter is equal to the specified string, the condition is true, run the command, otherwise run the next sentence. (Note that there are two equal signs)
For example, if "%1"=="a" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms
2. if exist filename command to be executed
If the specified file exists, the condition is true, run the command, otherwise run the next sentence.
For example, if exist config.sys edit config.sys
3. if errorlevel / if not errorlevel number command to be executed
If the return code is equal to the specified number, the condition is true, run the command, otherwise run the next sentence.
For example, if errorlevel 2 goto x2
All DOS programs return a number to DOS when running, called the error code errorlevel or return code. The common return codes are 0 and 1.
10. for Command
The for command is a relatively complex command, mainly used to execute commands in a loop within a specified range of parameters.
When using the FOR command in a batch file, use %%variable to specify the variable.
for {%variable│%%variable} in (set) do command
%variable specifies a single letter replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specify parameters or command-line switches for a specific command.
When using the FOR command in a batch file, use %%variable to specify the variable instead of %variable. The variable name is case-sensitive, so %i is different from %I
If command extensions are enabled, the following additional FOR command formats will be supported:
FOR /D %variable IN (set) DO command
If wildcards are included in the set, it means matching directory names instead of file names.
FOR /R path] %variable IN (set) DO command
path, and point to the FOR statement in each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.) character, enumerate the directory tree.
FOR /L %variable IN (start,step,end) DO command
%variable IN (file-set) DO command
FOR /F %variable IN ("string"
DO command FOR /F %variable IN ('command'
DO command Or, if the usebackq option is available:
FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ("string"
DO command FOR /F %variable IN ('command'
DO command filenameset is one or more file names. Before continuing to the next file in filenameset, each file has been opened, read, and processed.
Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more symbols. Then the for loop is called with the symbol string variable value found. By default, /F separates through the first blank symbol in each line of each file. Skip blank lines. You can replace the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:
eol=c - refers to the end of a line comment character (just one)
skip=n - refers to the number of lines to be ignored at the beginning of the file.
delims=xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs.
tokens=x,y,m-n - refers to which symbols of each line are passed to each iteration of for itself. This will lead to the format of additional variable names as a range. Specify the last character star of the m symbol string through the nth symbol,
then the additional variables will be allocated and accept the reserved text of the line after the last symbol parsing.
usebackq - specifies that the new syntax has been used in the following situations:
When executing a backquoted string as a command and the quote character is a literal string command and allows the use of double quotes to enclose file names in fi.
sample1:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do command
It will analyze each line in myfile.txt, ignore those lines starting with a semicolon, and pass the second and third symbols in each line to the for program body; delimited by commas and/or spaces. Please note that the statement of this for program body refers to %i to get the second symbol, refers to %j to get the third symbol, and refers to %k to get all the remaining symbols after the third symbol. For file names with spaces, you need to enclose the file name with double quotes. In order to use double quotes in this way, you also need to use the usebackq option, otherwise, the double quotes will be understood as being used to define a string to be analyzed.
%i is specifically explained in the for statement, and %j and %k are specifically explained through the tokens= option. You can specify up to 26 symbols in a line through tokens=, as long as you do not try to explain a variable higher than the letter 'z' or 'Z'. Please remember that FOR variables are single letters, case-sensitive, and global; at the same time, there cannot be more than 52 in use.
You can also use the FOR /F analysis logic on adjacent strings; the method is to enclose the filenameset between parentheses with single quotes. In this way, the string will be regarded as a single input line in a file.

. The article mentioned at the beginning that using batch files can simplify daily or repetitive tasks. So how to achieve it? Hehe, you will understand when you read on.

