BAT batch file basic format for loop

Source: Internet
Author: User
Tags eol ini

One, basic format

Executes a specific command on each file in a set of files.

For%variable in (set) do command [Command-parameters]

%variable specifies a single letter replaceable parameter.

(set) to specify one or a set of files. You can use wildcard characters.

command specifies the commands that are executed for each file.

Command-parameters

Specify a parameter or command-line switch for a specific command.

When you use the for command in a batch program, specify the variable using the%%variable

Instead of using%variable. Variable names are case-sensitive, so%i are different from%i.

Parameters: For there are 4 parameters/d/l/r/f Their role in the following example

Second, parameter/d

FOR/D%variable in (set) do command [Command-parameters]

If the set contains wildcard characters, it specifies that the directory name is matched instead of the filename.

For example:

@echo off

FOR/D%%i in (c:*) do echo%%i

Pause

After the operation, will be the C-packing directory to print all the directories, will not print files

Example:

@echo off

FOR/D%%i in (???) do echo%%i

Pause

will be the current directory with the directory name only 1-3 letters, will be displayed, no will not show

The * number denotes any n characters, while the? represents just any one character

Third, parameter/R

FOR/R [[Drive:]path]%variable in (set) do command [Command-parameters]

Check the [drive:]path-rooted directory tree to point to the for statement in each directory.

If the directory specification is not specified after/R, the current directory is used. If the set is only one single point (.) Character

The directory tree is enumerated.

/R can read all the filenames in the current or your specified path, and note the file name,

Note 2 points:

The filename in 1.set, if it contains a wildcard character (? or *), enumerates all files in the directory specified by the/R parameter and all of the subdirectories below it that match the set, and no directories without a matching file are enumerated

2. Conversely, if the set contains a specific file name and does not contain a wildcard character, the directory tree is enumerated, regardless of whether the specified file in the set exists

Example:

@echo off

FOR/R D:%%i in (*.exe) do echo%%i

Pause

I'll put all the files in D on the end of EXE.

@echo off

FOR/R D:%%i in (boot.ini) do echo%%i

Pause

Enumerate Directories in D disk that have Boot.ini files

Four.

FOR/L%variable in (start,step,end) do command [Command-parameters]

The set represents a sequence of numbers in increments from start to finish. Therefore, (1,1,5) will produce a sequence

1 2 3 4 5, (5,-1,1) will produce the sequence (5 4 3, 2 1)

Start is the starting value, Stup is the increment, end value,

Example:

@echo off

FOR/L%%i in (1,1,5) do echo%%i

Pause

Will list 12345 of these values

@echo off

FOR/L%%i in (1,1,5) do start cmd

Pause

will open 5 cmd windows.

Five, Parameter/F

Iteration and File resolution

Use file resolution to process command output, string and file content, use an iteration variable to define the content or string to check, and use various option options to further

Modify resolution, and use the Options token option to specify that tokens should be passed as iteration variables

Note: When the token option is not used,/F will only check the first token

The file parsing process includes reading output, string or file contents, dividing it into separate lines of text, and parsing each row into 0 or more tokens

Let it call the for loop by setting the value of the iteration variable to the token, by default,/F passes the first blank separator for each line of each file, skipping the blank line

Command format:

for/f ["Options"]%variable in (file-set) do command [Command-parameters]

for/f [' Options ']%variable in (' String ') do command [Command-parameters]

for/f ["Options"]%variable in (' Command ') do command [Command-parameters]

The quoted string "Options" includes one or more

Specifies the keywords for different resolution options:

EOL=C Specifies the end of a comment character (just one)

Skip=n refers to the number of rows ignored at the beginning of a file

DELIMS=XXX Specifies the delimiter set, which replaces the default delimiter set for the spaces and tabs

Tokens=x,y,m-n refers to which of each row conforms to the for itself that is passed to each iteration, which is the assignment of an extra name, m-n format is a range

Specify MTH with the nth symbol, and if the best one in the symbol string is the asterisk number, then the extra variable will be allocated and accepted after the best symbol resolution, and the reserved text for the line.

Tested, this parameter can only distinguish 31 fields

USEBACKQ specifies that the new syntax is already used in the following class cases:

Executes a string of post quotes as a command and a single

The quote character is a literal string command and is allowed in the File-set

Use double quotes to expand the file name.

Example:

@echo off

REM first creates temporary files, uses up delete >test.txt

echo Comment Line, this is a temporary file, run out of delete >text.txt

Echo >> Test.txt

echo 21,22,23,24,25,26 >> test.txt

echo 31-32-33-34-35-36 >> test.txt

For/f "eol=; tokens=1,3* delims=,-"%%i in (test.txt) do echo%%i%%j%%k

Pause

Del Text.txt

Run......

Explain:

eol=; Line end Comment Line at beginning of semicolon

tokens=1,3-assigns variable%%i,%%j,%%k to the 1th, 3rd, and remaining sections of each row respectively.

delims=,-(a space after the minus sign) is delimited by a comma minus sign and a space, and the space must be placed at the end

Example:

@echo off

for/f "Skip=4 delims="%%i in (' Net user ') do echo%%i

Pause

So your entire account name comes out. Enclose the contents of the brackets in two single quotes, which means that when the command executes, the for returns the result of the command

, add that "delims=" is to let the line of the space can be displayed on the line, without adding on the default display only the left column of space, should be the default separator space or called a tab

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.