DOS Batch Advanced Tutorial Chapter II DOS loop for command details _dos/bat

Source: Internet
Author: User
Tags eol

For the first time, I tell you novice friends, if you have any orders do not understand, directly under the cmd input:

Name/? This format looks at the help files given by the system, such as for/? Will show the help of the for command all! Of course, many beginners can not understand .... That's why there are so many batches of articles!!!! I also take care of the rookie, the for order in my own way to explain!

Official start:

First, basic format

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

%%variable specifies a single letter to represent replaceable parameters.
(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 specifies a parameter or command-line switch for a particular command.

Parameter: For has 4 parameters/d/l/r/f Their function I'll explain it in the following example
Now start talking about the meaning of each parameter

Ii. 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.

If Set (that is, the related file or command written above) contains wildcard characters (* and?), the specified Command is executed for each directory that matches the set, not the filegroup in the specified directory.

This parameter is mainly used for directory search, do not search for files, see such examples

@echo off
for/d%%i in (c:\*) do echo%%i
pause

The operation will be the C-packing directory of all the directory name printed out, and the file name does not show!

In one, for example we're going to put the name of the folder under the current path with just 1-3 letters.

@echo off
for/d%%i in (???) do echo%%i
pause

In this case, if you have a directory name in the current directory with only 1-3 letters, it will be displayed, no, not shown.

This explains the role of the * and the number, the * number represents any n characters, and the number only represents any one character

Know the role, give everyone a thinking topic!

@echo off
for/d%%i in (Windows?) do echo%%i
pause

Save to C-disk execution, what will be shown? Look at it! Display: Windows
The/d parameter can only display the directory name under the current directory, this should be noted!

Iii. 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 no directory is specified after/R, the current directory is used. If the set is only one single point (.) character, the directory tree is enumerated.

Recursion

As we know above,/d can only display the directory name under the current path, so now this/R is also related to the directory, what could he do? He is much stronger than D/a!
He can read the file name of the current or your designated path, note that the file name, what to look at the example!

Please note 2 points:

If the file name in the set contains a wildcard character (? or *), enumerates all files in the directory specified by the/R parameter and the following subdirectories that match the set, and directories without matching files are not enumerated.
Conversely, if the set contains a specific file name and does not contain a wildcard character, the tree is enumerated (that is, enumerating the directory and all subdirectories below it), regardless of whether the specified file exists in the set. This is true of the single point (.) enumeration tree mentioned above, which represents the current directory or a single file.

Cases:

@echo off
for/r c:\%%i into (*.exe) do echo%%i
pause

We're going to save this bat to D. Anywhere and then execute, I'll see, he put the C-packing directory, and subdirectories of each directory, all of the following EXE files are listed!!!!

Cases:

@echo off
for/r%%i in (*.exe) do @echo%%i
pause

The parameters are different! This command is not preceded by the C:\ that is the search path, so that he will be the current directory for the search path, such as you this bat you put him in the D:\test directory to execute, then he will be the D:\test directory and his following subdirectory of the entire EXE file list to!!!

Cases:

@echo off
for/r c:\%%i into (boot.ini) do echo%%i
pause

Run this example to find that all directories in the C disk are enumerated, in order to enumerate only the directories that exist in Boot.ini, it can be changed to the following:

@echo off
for/r c:\%%i into (boot.ini) do if exist%%i echo%%i
Pause

It's nice to search for files with this command ...

We should understand this parameter, or a fun order!

Iv. Parameters/L

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 of 1 2 3 4 5, (5,-1,1) will produce a sequence (5 4 3 2 1).

Use an iteration variable to set the starting value (start#), and then step through a set of ranges of values until the value exceeds the set termination value (end#). /L The iteration variable is executed by comparing the start# with the end#. If the start# is less than end#, the command is executed. If an iteration variable exceeds end#, the command interpreter exits the loop. You can also use negative step# to progressively execute values in this range in descending numeric values. For example, (1,1,5) generates a sequence of 1 2 3 4 5, whereas (5,-1,1) generates a sequence (5 4 3 2 1). The syntax is:

Look at this shows a little dizzy bar! Let's see the example, we're not dizzy!

@echo off
for/l%%i in (1,1,5) do @echo%%i
pause

Save execution See effect, he will print from 1 2 3 4 5 such 5 digits (1,1,5) This parameter means that from 1 start every time plus 1 until 5 terminates!

Wait to faint, print a number has p ... Good to meet everyone, look at this example

@echo off
for/l%%i into (1,1,5) do start cmd
pause

After the implementation is not startled, how many 5 cmd window, hehe! If you change that (1,1,5) to (1,1,65535) will have what result, I first tell you, will open 65,535 cmd window .... So much you don't crash count you strong!

Of course, we can also change that start CMD to MD%%i so that we will set up a specified directory!!! Name is 1-65535

After reading this parameter that I've been given a destructive nature to, let's look at the last argument.

V. Parameters/F

\ iteration and file resolution

Use file resolution to process command output, strings, and file contents. Use the iteration variable to define the content or string to check, and further modify the resolution using various options choices. Use the Options token option to specify which tokens should be passed as iteration variables. Please note that if you do not use the token option,/F will only check the first token.

The file resolution process involves reading the output, string, or file contents, dividing it into separate lines of text, and then parsing each row into 0 or more tokens. The For loop is then invoked by the value of the iteration variable set to the token. By default,/F passes the first blank separator for each row of each file. skipping blank lines.

The detailed help format is:

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 keywords that specify different parsing options. The key words are:

Eol=c-refers to the end of a line comment character (just one)
Skip=n-refers to the number of rows ignored at the start of the file.
Delims=xxx-refers to the delimiter set. This replaces the default delimiter set for spaces and tabs.
Tokens=x,y,m-n-refers to which symbol of each line is passed to the for itself for each iteration. This results in an assignment of the extra variable names. The m-n format is a range. Specify MTH by nth symbol. If the last character in the symbol string is asterisk, the extra variable is allocated and accepts the reserved text of the line after the last symbol resolution. Tested, this parameter can only distinguish 31 fields.

USEBACKQ-Use the back quotation mark (the key on the left of the number 1 on the keyboard ').
When parameter USEBACKQ is not used: File-set represents a file, but cannot contain spaces
Double quotes represent strings, that is, "string"
Single quotation marks represent execution commands, that is, ' command '
When using parameter usebackq: File-set and "File-set" both represent files
When there is a space in the file path or name, you can enclose it in double quotes
Single quotes represent strings, that is, ' string '
The following quotation marks indicate the command execution, that is, ' command '

The above is a direct copy of the Help information obtained with the for/? command.
Oh, my gosh! I'll give you an example to help you understand these parameters!


For command Example 1:****************************************

@echo off
rem First create temporary file Test.txt
Echo; Comment Line, this is temporary file, delete >test.txt
Echo 11 paragraph 12 Paragraph 13 14 Paragraph 15 paragraph 16 paragraph >>test.t XT
Echo 21, 22, 23, 24, 25, 26 segment >>test.txt
Echo 31-32 Segment-33 Segment-34 Segment-35 Segment-36 segment >>test.txt
for/f " eol=; tokens=1,3* delims=,-"%%i in (test.txt) do echo%%i%%j%%k
Pause
Del test.txt

Run Display results:

11 paragraph 13 Paragraph 14 paragraph 15 paragraph 16 paragraph
21 Paragraph 23 Paragraph 24 paragraph 25 paragraph 26 paragraph
31 Paragraph 33 Paragraph 34 paragraph-35 paragraph-36 paragraph
Please press any key to continue ...

Why is that? Let me explain:
eol=; Behavior comment lines beginning with semicolons
TOKENS=1,3* assigns the 1th, 3rd, and remaining fields of each row to the variable%%i,%%j,%%k
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


For command Example 2:****************************************

@echo off
for/f "eol= delims="%%i in (test.txt) do echo%%i
Pause

Run will show test.txt all content, including comment lines, not explained Ha.

For command Example 3:****************************************

The other/F parameter can also be seen in the output command result of this example

@echo off
for/f the "delims="%%i in (' Net user ') does @echo%%i
pause

So you have the entire account name of the computer out of the extension of the contents of the two single quotes to indicate that when the command to execute, for will return the command of the results of each row, plus that "delims=" is to let me the space line can be displayed in the entire line, without adding just the left column of space to show!

Basically finished with the basic usage of for ... If you've seen a for-system help, you'll find that there are some very defined variables underneath, which I'm not going to tell you about. Everyone is tired, you are not tired I am tired ...

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.