Explanation of Dos batch processing-1

Source: Internet
Author: User
Tags echo command eol tag name
Recently, the discussion on batch processing technology is very popular, and there are also a lot of good batch processing programs released. However, if you do not have some knowledge, I am afraid it is not easy to see.
Understanding and understanding of these batch processing files, let alone writing them by yourself. The old saying cloud: "It is better to teach people to fish than to teach people to fish ." Because the Internet seems to be
There is not a complete teaching material, so I took some time to write this article <simple batch processing tutorial> to new friends.
The freedom and sharing of friends.

A batch file is a non-formatted text file that contains one or more commands. Its file extension is. bat or. cmd. Enter
Batch File Name. When you double-click the batch file, the system will call cmd.exe to run one by one according to the order in which the commands in the file appear.
They. The use of batch files (also known as batch processing programs or scripts) can simplify daily or repetitive tasks. Of course, the main
The main content is to introduce some practical application of batch processing in intrusion. For example, we will refer to using batch processing files to patch the system
Program. Let's start our batch processing learning journey.

1. Introduction to simple batch processing internal commands

1. Echo command
Enable or disable the request echo function or display messages. If no parameters exist, the echo command displays the current echo settings.
Syntax
Echo [{on | off}] [message]
Sample: @ echo off/echo hello world
In actual application, we will combine this command with the redirection symbol (also known as the pipeline symbol) to input some commands to a specific
Format file. This will be reflected in future examples.

2. @ command
Indicates that the command after @ is not displayed. During the intrusion process (for example, you can use batch processing to format the enemy's hard disk ),
.
Sample: @ echo off
@ Echo Now initializing the program, please wait a minite...
@ Format X:/q/u/autoset (the/y parameter cannot be used for the format command. Fortunately, Microsoft has reserved the autoset parameter.
For us, the effect is the same as that of/y .)

3. Goto command
Specify to jump to the tag. After the tag is found, the program processes the commands starting from the next line.
Syntax: goto label (label is a parameter that specifies the rows in the batch processing program to be switched .)
Sample:
If {% 1 }={} goto noparms
If {% 2 }={} goto noparms (if you do not understand the if, % 1, and % 2 here, skip the step first, which will be explained in detail later .)
@ Rem check parameters if null show usage
: Noparms
Echo Usage: monitor. bat ServerIP PortNumber
Goto end
The tag name can be used as needed, but it is better to have a meaningful letter. Add a letter before it to indicate that the letter is a tag, and The goto command is the root.
From this: to find the next step to jump there. It is better to have some explanations so that others seem to understand your intention.

4. Rem command
The annotation command is equivalent to/* -------- */in the C language. It is not executed, but serves as a comment for others to read and modify.
Rem Message
Sample: @ Rem Here is the description.

5. Pause command
When running the Pause command, the following message is displayed:
Press any key to continue...
Sample:
@ Echo off
: Begin
Copy a: *. * d: \ back
Echo Please put a new disk into driver
Pause
Goto begin
In this example, all files on drive A are copied to drive d: \ back. When the displayed comment prompts you to put another disk into drive,
The pause command suspends the program so that you can change the disk and press any key to continue processing.

6. Call Command
Call another batch processing program from one batch processing program without terminating the parent batch processing program. The call Command accepts the labels used as the call target. If
Call is used outside the current or batch file, and it will not work in the command line.
Syntax
Call [[Drive:] [Path] FileName [BatchParameters] [: label [arguments]
Parameters
[Drive:} [Path] FileName
Specifies the location and name of the batch processing program to be called. The filename parameter must have the. bat or. cmd extension.

7. start command
Call an external program. All the DOS commands and command line programs can be called by the start command.
Common Intrusion parameters:
Minimum window size when MIN starts
SEPARATE starts a 16-bit Windows program in a SEPARATE space
HIGH starts applications in the HIGH priority category
REALTIME starts applications in the REALTIME priority category
WAIT starts the application and waits for it to end
Parameters: these are parameters sent to the command/program.
When the executed application is a 32-bit GUI application, CMD. EXE returns a command prompt before the application is terminated. If it is executed in the Command script,
This new action will not happen.
8. choice command
Choice uses this command to allow users to enter a single character to run different commands. The/c: parameter should be added for use, and a prompt should be written after c: can be entered
There is no space between them. Its return code is 1234 ......
For example: choice/c: dimethyl defrag, mem, end
Will display
Defrag, mem, end [D, M, E]?
Sample:
The content of Sample. bat is as follows:
@ Echo off
Choice/c: dimethyl defrag, mem, end
If errorlevel 3 goto defrag (the highest error code should be determined 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, defrag, mem, end [D, M, E]? You can select d m e, and then the if statement will make a judgment. d indicates that the execution label is
Defrag's program segment, m indicates the program segment whose execution label is mem, and e indicates the program segment whose execution label is end.
The program jumps to the end label, and the program displays good bye. The file ends.

9. If command

If indicates whether the specified conditions are met, and then different commands are executed. 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:
If {% 1 }={} goto noparms
If {% 2 }={} goto noparms

2. if exist file name command to be executed
If a 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
When the DOS program runs, a number is returned to DOS, which is called the error level or return code. Common return codes are 0 and 1.

10. for command
The for command is a complex command. It is mainly used to execute commands cyclically 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 [CommandLineOptions]
% Variable specifies a single letter replaceable parameter.
(Set) specifies one or more files. Wildcard characters can be used.
Command specifies the command executed on each file.
Command-parameters specifies a parameter or command line switch for a specific command.
When using the FOR command in a batch file, use % variable to specify the variable
Instead of % variable. Variable names are case sensitive, so % I is different from % I

If the command extension is enabled, the following additional FOR command formats are subject
Supported:

FOR/D % variable IN (set) DO command [command-parameters]

If the set contains wildcards, it is specified to match the directory name instead of the file name.
Name match.

FOR/R [[drive:] path] % variable IN (set) DO command [command-

Check the directory tree with [drive:] path as the root and point
FOR statement. If no directory is specified after/R, the current
Directory. If the set is only a single point (.) character, the directory tree is enumerated.

FOR/L % variable IN (start, step, end) DO command [command-para

This set indicates a sequence of numbers from start to end in incremental form.
Therefore, (, 5) will generate the sequence 1 2 3 4 5, (5,-) will generate
Sequence (5 4 3 2 1 ).

FOR/F ["options"] % variable IN (file-set) DO command
FOR/F ["options"] % variable IN ("string") DO command
FOR/F ["options"] % variable IN (command) DO command

Or, if usebackq is available:

FOR/F ["options"] % variable IN (file-set) DO command
FOR/F ["options"] % variable IN ("string") DO command
FOR/F ["options"] % variable IN (command) DO command

Filenameset is one or more file names. Continue to filenameset
Before the next file, each file has been opened, read, and processed.
Processing includes reading files, dividing them into lines of text, and then dividing each line
It is parsed into zero or more symbols. Then use the variable value of the found symbol string
Call the For loop. By default,/F is separated by each row of each file.
. Skip blank lines. You can specify the Optional options"
The parameter replaces the default parsing operation. This quoted string contains one or more
Specify keywords for different resolution options. These keywords are:

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 space and the hop key.
Default delimiter set.
Tokens = x, y, m-n-indicates which symbol of each row is passed to each iteration.
For itself. This causes
Format is a range. Specify m using the nth symbol
The asterisks of the last character in the symbol string,
Then the extra variables will be parsed In the last symbol
Distribute and accept reserved text of rows.
Usebackq-specify that the new syntax is used in the following situations:
In the format
The quotation mark character is a text string command and allowed in the fi
Use double quotation marks to expand the file name.

Sample1:
FOR/F "eol =; tokens = 2, 3 * delims =," % I in (myfile.txt) do command

Analyzes each row in myfile.txt, ignores the rows headers with semicolons
The second and third symbols in each line are passed to the for program body. Use commas and/or
Space separator. Please note that the for program body statement references % I
Get the second symbol, reference % j to get the third symbol, reference % k
To obtain all the remaining symbols after the third symbol. For files with spaces
Name, You need to enclose the file name in double quotes. To make
Use double quotation marks. You also need to use the usebackq option. Otherwise, the double quotation marks will
It is understood that it is used to define a string to be analyzed.

% I is specified in the for statement. % j and % k are
Tokens = the option is specifically described. You can use tokens = a line
A maximum of 26 characters can be specified. If you do not try to figure out a symbol higher than the letter z or
Z variable. Remember that the FOR variable is a single letter, case sensitive, and global;
At the same time, no more than 52 instances are in use.

You can also use FOR/F to analyze the logic on adjacent strings. The method is,
Enclose the filenameset between the brackets in single quotes. In this way, the character
Strings are treated as a single input line in a file.

Finally, you can use the FOR/F command to analyze

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.