Batch Processing tutorial (I)

Source: Internet
Author: User
Tags echo command echo message eol
Proficient in Batch Processing
Recently, many viruses with weak passwords have become popular on the Internet. For example, the worm. dvldr worm, which has been very popular some time ago, is a typical one. This
The common cause of these viruses is to use batch processing to establish an IPC $ connection, so as to guess the Administrator's password to control the server. Disease
The virus consists of several files and several complex batches. Batch processing is not really a programming, but some of its ideas and programming comparison
Approximate. Through online communication with some beginner friends, I found that they are very interested in batch processing and learn more or less about the usage of some commands.
But lack of systematic understanding, so I wrote this tutorial specially so that interested friends can have a general understanding of batch processing and
Write your own batch processing.
This tutorial consists of four parts: The first part is the dedicated Command for batch processing, the second part is the special symbol and batch processing, and the third part is
Batch Processing and variable. The fourth part is the complete case. Because the tutorial is relatively long, we will be divided into two series in the magazine.
Welcome to the first and second sections.

Part 1: dedicated commands for Batch Processing
A batch file is a collection of commands in a certain order into an executable text file with the extension bat. These commands are collectively referred to
The following describes the batch processing commands.
1. Rem
Rem is a comment command, which is generally used to add annotations to the program. The content after this command will not be displayed and executed during program execution.
. Example:
Rem, what you see now is the annotation. This sentence will not be executed. The content explained in subsequent examples will be placed after REM.
Please note.
2. Echo
ECHO is a echo command. The main parameters include off and on. Generally, Echo message is used to display a specific message. Example:
Echo off
REM and above indicate that the ECHO is disabled and the executed command is not displayed.
Echo: this is the message.
REM and above indicate that "this is the message" is displayed.
Execution result:
C: \> echo. bat
This is the message.
3. Goto
Goto indicates the jump. In batch processing, a label can be constructed with ": XXX" and then executed directly with "Goto:" label.
. Example
: Label
Above REM is the label.
Dir c :\
Dir D :\
Goto label
The above program jumps to the label and continues execution.
4. Call
The call command can call another batch during batch processing. After another batch is executed, the original batch is executed again.
. Example:
Batch Processing 2. Bat content is as follows:
Echo. This is the content of 2.
Batch Processing 1. Bat content is as follows:
Echo: This is 1.
Call 2.bat
Echo 1 and 2 are displayed completely
The execution result is as follows:
C: \> 1.bat
This is the content of 1.
This is the content of 2.
The content of 1 and 2 is displayed completely.
5. Pause
Pause stops the execution of system commands and displays the following content. Example:
C: \> pause
Press any key to continue...
6. If
If condition judgment statement, the syntax format is as follows:
If [not] errorlevel number command
If [not] string1 = string2 command
If [not] exist filename command
Note:
[Not] returns the reverse value of the returned result, that is, "If not.
Errorlevel is the exit value returned after the command is executed.
The number of the exit value ranges from 0 ~ 255. The order of values should be large to small. The returned value is greater than or equal to the specified value.
The condition is true.
String1 = string2 string1 and string2 are both character data. The case sensitivity of English characters is different.
The equal sign must be two (absolutely equal), and the command
Exist filename indicates the existence of a file or directory.
The IF errorlevel statement must be placed after a command. After the command is executed, if errorlevel is used to determine the return value of the command.
Example:
1. If [not] errorlevel number command
Checks the return value after the command is executed.
Echo off
Dir Z:
If the exit code is 1 (unsuccessful), Rem jumps to Title 1 for execution.
If errorlevel 1 GOTO 1
If the exit code is 0 (successful), Rem jumps to the title 0 for execution.
If errorlevel 0 goto 0
: 0
Echo Command executed successfully!
After the REM program is executed, jump to the title exit and exit.
Goto exit
: 1
Echo command execution failed!
After the REM program is executed, jump to the title exit and exit.
Goto exit
: Exit
Rem is the exit of the program.
2. If string1 = string2 command
Checks the value of the current variable to make a judgment.
Echo off
If % 1 = 2 goto No
Echo variables are equal!
Goto exit
: No
Echo variables are not equal
Goto exit
: Exit
You can see the effect as follows: C: \> test. Bat number
3. If [not] exist filename command
Identify specific files to make judgments
Echo off
If not exist autoexec. Bat GOTO 1
ECHO file exists successfully!
Goto exit
: 1
Failed to exist ECHO file!
Goto exit
: Exit
This batch processing can be performed on disk C and disk D to see the effect.
7.
The for command is a special command to execute the command cyclically. At the same time, the for command can also be used in the loop. This article
We will introduce the basic usage and will not apply the loop. We will explain the applied loop later. The for command in batch processing is as follows:
For [% C] In (SET) do [command] [arguments]
The command in the command line is as follows:
For [% C] In (SET) do [command] [arguments]
Common parameters:
/L this set indicates a sequence of numbers starting from the beginning to the end in incremental form. Therefore, (, 5) will generate a sequence of 1 2 3 4 5, (5 ,-
1, 1) will generate a sequence (5 4 3 2 1 ).
/D if the set contains wildcards, it is specified to match the directory name instead of the file name.
/F read data from the specified file as a variable
EOL = C-refers to the end of a line comment character (just one)
Skip = N-indicates the number of rows ignored at the beginning of the file.
Delims = xxx-refers to the delimiter set. This replaces the default delimiter set of spaces and the hop key.
Tokens = x, y, M-n-indicates which symbol of each row is passed to the for itself of each iteration. This leads to the allocation of additional variable names.
The M-N format is a range. Use the nth symbol to specify MTH. If the last character asterisk in the symbol string is used, the additional variable
The reserved Text of the row will be allocated and accepted after the last symbol is parsed.
Usebackq-specify that the new syntax is used in the following situations: run a string with quotation marks and a single quotation mark character is
The text string command allows you to use double quotation marks to expand the file name in filenameset.
Here is an example:
For/F "EOL =; tokens = 2, 3 * delims =," % I in (myfile.txt) Do @ echo % I % J % K
Analyzes each row in myfile.txt, ignores the rows headers with semicolons, and passes the second and third symbols in each row to
Program body. Use commas (,) and/or spaces to specify the delimiter. Please note that the for program body statement references % I to obtain the second symbol, reference
% J to get the third symbol, reference % K to get all the remaining symbols after the third symbol. For file names with spaces, you must use
Double quotation marks enclose file names. To use double quotation marks in this way, you also need to use the usebackq option. Otherwise, double quotation marks will
It is understood that it is used to define a string to be analyzed.
% I is specifically described in the for statement. % J and % K are specifically described through the tokens = option. You can use tokens =
A row can contain up to 26 characters. If you do not try to figure out a variable higher than the 'z' or 'Z' letter. Keep in mind that the for variable name is too large
In lowercase, it is common. Moreover, no more than 52 characters can be used at the same time.
You can also use for/F to analyze the logic on adjacent strings. The method is to enclose the filenameset between parentheses with single quotation marks.
. In this way, the string is treated as a single input line in a file. Finally, you can use the for/F command to analyze command input.
Output. The method is to convert the filenameset between parentheses into an anti-string. This string will be passed to
The output of the sub-cmd. EXE is captured into the memory and analyzed as a file. Therefore, the following example:
For/F "usebackq delims =" % I in ('set') Do @ echo % I
The names of environment variables in the current environment are enumerated.
The following is a simple example to illustrate the difference between/L and no parameter:
Delete the 1.txt 2.txt 3.txt 4.txt 5.txt File
Example:
Echo off
For/L % F in (1, 1, 5) Do del % F. txt
Or
For % F in (1, 2, 3, 4, 5) Do del % F. txt
The execution results of the preceding two commands are the same as follows:
C: \> Del 1.txt
C: \> Del 2.txt
C: \> Del 3.txt
C: \> Del 4.txt
C: \> Del 5.txt
8. setlocal
Start localization of Environment Changes in the batch file. After setlocal is executed
The environment changes are limited to batch files. To restore the original settings, you must execute
Row endlocal. When the end of a batch file is reached
If the setlocal command is not executed, an implicit endlocal is
Run. Example:
@ Echo off
Set path/* view the environment variable path
Pause
Setlocal
Set Path = E: \ tools/* reset the environment variable path
Set path
Pause
Endlocal
Set path
From the above example, we can see that the environment variable path is the default path of the system when it is displayed 1st times. It is displayed
E: \ tools, but when endlocal is used, we can see that it is restored to the default path of the system. However, this setting only runs in the batch.
. After the batch processing is completed, the environment variable path will be restored.
9. Shift
The shift command allows more than 10 commands on the command to be used (% 0 ~ % 9) Examples of the above replaceable parameters:
Echo off
Echo % 1% 2% 3% 4% 5% 6% 7% 9
Shift
Echo % 1% 2% 3% 4% 5% 6% 7% 9
Shift
Echo % 1% 2% 3% 4% 5% 6% 7% 9
The execution result is as follows:
C: \> shift. Bat 1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10 11
The above are nine batch processing commands Based on win2000.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.