Instructions and learning skills for using for statements in Batch Processing

Source: Internet
Author: User

The function of the for command in batchcompute bat is very good and can save a lot of efficiency. It can read Article If you do not have a single line, you can run the ping command cyclically. If you do not want to learn about batch processing, it will be a pity. I hope you can take a look at the examples of the actual application of the for command, if you only look at the simple basic usage method, but do not have the practical application, there will be no more basic, you must first look at the instance to look at the basis, take the problem to look at the basis, I believe you will be able to get started soon. There are a lot of such resources on this site that you can search
Next I will sort out the basic parameters of the for command. When learning, you can play for/under DOS /? To view help.

For
Run the specified command on each file in a group of files.

Syntax
For {% variable | % variable} In (SET) docommand [commandlineoptions]
Parameters
{% Variable | % variable}
Required. Represents replaceable parameters. Run the for command at the command prompt using % variable. Use % variable in Batch Processing
File. Variables must be case sensitive and must be expressed with Alpha values, such as % A, % B, or % C.

(SET)

Required. Specify one or more files, directories, value ranges, and text strings to be processed using the specified command. Brackets are required.

Command

Required. Specifies the command to be executed for each file, directory, value range, or text string in the specified (SET.

Commandlineoptions

Specifies any command line options to be used with the specified command.

/?

Display help at a command prompt.

Note
• Use

You can use the for command in a batch file or directly from a command prompt.

• Use Batch Processing Parameters

The following attributes apply to the for command:

• The for command replaces % variable or % variable with each text string specified in set until the command finishes processing all

File.

• Forvariable names are case-sensitive and global, and each activity contains no more than 52 names.

• To avoid obfuscation of batch processing parameters % 0 to % 9, you can use any character for variable except numbers 0 to 9. For simple

Batch file. A single character can take effect, for example, % F.

• Multiple Variable values can be used in complex batch files to differentiate different replaceable variables.

• Specify a group of Files

The Set parameter can represent one or more file groups. Wildcard characters (* And?) can be used ?) Specifies a file group. The following are valid file groups:

(*. DOC)

(*. Doc *. txt *. Me)

(Jan *. docjan *. rptfeb *. docfeb *. rpt)

(AR ?? 1991. * AP ?? 1991 .*)

When you use the for command, the first value in the Set replaces % variable or % variable, and then the specified command processes the value.

This continues until all files (or file groups) that correspond to the set value are processed.

• Use the in and do keywords

In And do are not parameters, but must be used with. If any keyword is omitted, an error message is displayed.

.

• Use other for formats

If you enable command extension (this is the default setting), other formats of for are supported:

• Directory only

If set contains wildcards (* And ?), Execute

Command. Syntax:

For/d {% | %} variablein (SET) docommand [commandlineoptions]

• Recursion

Go to the root directory tree [drive:] path and execute the for statement in each directory of the tree. If no directory is specified after/R

Current directory. If set is only a period (.), only the directory tree is enumerated. Syntax:

For/R [[drive:] path] {%%|%} variablein (SET) docommand [commandlineoptions]

• Range of iterations

Use the iteration variable to set the start value (start #), and then gradually execute a set of range values until the value exceeds the set end value (END #)

. /L execute iteration variables by comparing start # And end. If start # is smaller than end #, the command is executed.

If the iteration variable exceeds end #, the command will explainProgramExit this loop. You can also use the negative step # To gradually execute it in a descending value.

Value in this range. For example, (12345, 5) generates the sequence 54321, while (5,-) generates the sequence ). Syntax:

:

For/L {% | %} variablein (start #, step #, end #) docommand [commandlineoptions]

• Iteration and File Parsing

Use File Parsing to process command output, strings, and file content. Use iteration variables to define the content or string to be checked, and use

Parsingkeywords option. Use the parsingkeywords token option to specify which tokens should be used

Iteration variable transfer. Note: When the token option is not used,/F checks only the first token.

The file parsing process includes reading output, string, or file content, dividing it into independent lines of text, and parsing each line into zero or more lines.

Token. Then, call the for loop by setting the variable value as the token. By default,/F transmits

A blank separator. Skip empty rows. Syntax differences:

For/f ["parsingkeywords"] {%%|%} variabelin (filenameset) docommand [commandlineoptions]

For/f ["parsingkeywords"] {%%|%} variablein ("literalstring") docommand

[Commandlineoptions]

For/f ["parsingkeywords"] {%%|%} variablein ('command') docommand [commandlineoptions]

The filenameset parameter specifies one or more file names. Each file will be opened, read, and processed before execution.

The next file in filenameset. To overwrite the default parsing behavior, specify "parsingkeywords ". This is enclosed in quotation marks.

It contains one or more keywords to specify different resolution options.

If usebackq is used, use one of the following syntax:

For/f ["usebackqparsingkeywords"] {%%|%} variablein ("filenameset") docommand

[Commandlineoptions]

For/f ["usebackqparsingkeywords"] {%%|%} variablein ('literalstring') docommand

[Commandlineoptions]

For/f ["usebackqparsingkeywords"] {%%|%} variablein ('command') docommand

[Commandlineoptions]
This article is from a very batch processing Forum. For more exciting articles, visit bbs.verybat.cn.

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.