China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 13:22
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Very good tutorial on for. After reading this, you're guaranteed to know how to use for View 6,699 Replies 20
Original Poster Posted 2008-07-31 20:37 ·  中国 广东 珠海 电信
新手上路
Credits 6
Posts 1
Joined 2008-06-29 22:24
18-year member
UID 120927
Gender Male
Status Offline
Batch For Command Explanation

The FOR command is basically used to process text, but there are also some other useful functions!

Take a look at its basic format (here I am citing the format in batch processing; in the command line, only one % sign is needed)

FOR parameter %% variable name IN (related files or commands) DO executed command

Parameters: FOR has 4 parameters /d /l /r /f, and their functions are explained below with examples

%% variable name: This variable name can be lowercase a-z or uppercase A-Z, they are case-sensitive, and FOR will assign each read value to it;

IN: The format of the command, just write it as it is;

(related files or commands): What FOR reads and assigns to the variable, see the example below

do: The format of the command, just write it as it is!

The executed command: Write what operation to perform on each variable value here.

You can enter for /? in CMD to see the help provided by the system! Compare

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

%%variable specifies a single letter replaceable parameter.

(set) specifies one or a group of files. Wildcards can be used.

command specifies the command to be executed for each file.

command-parameters specifies parameters or command line switches for a specific command.

Now start to explain the meaning of each parameter

/d

Only for directories

If Set (that is, the "related files or commands" I wrote above) contains wildcards (* and ?), it will execute the specified Command for each directory that matches Set (instead of the group of files in the specified directory).

System help format: FOR /D %%variable IN (set) DO command

It is mainly used for directory search and will not search for files. Look at such an example

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

Save it in the root directory of drive C and execute it, and it will print out all the directory names under the C drive, and none of the file names will be displayed!

Another one, for example, we want to print out the names of folders in the current path that have only 1-3 letters

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

In this way, if there are directories in your current directory with only 1-3 letters, they will be displayed; if not, nothing will be displayed.

Thinking question:

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

Save it to the C drive and execute it, what will be displayed? Just see for yourself!

The /D parameter can only display the directory names in the current path, everyone should pay attention to this!

/R

Recursive

Enter the root directory tree [Drive:]Path, and execute the for statement in each directory of the tree. If no directory is specified after /R, the current directory is considered. If Set is just a period (.), only the directory tree is enumerated.

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

We know above that /D can only display the directory names in the current path, so now this /R is also related to directories, what can it do? Don't worry, it is much stronger than /D!

It can read all the file names under the current or the specified path, note that it is the file name, see the example for what it is used for!

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

Let's save this BAT in any place on drive D and then execute it, and we will see that it lists all the EXE files under the root directory of drive C and in each subdirectory of the directory. Here, c:\ is the directory.

Another one

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

The parameters are different. This command does not add that C:\ in front, that is, the search path, so it will use the current directory as the search path. For example, if you put this BAT in the d:\test directory and execute it, then it will list all the EXE files in the D:\test directory and its subdirectories!!!

/L

Iterate over a range of numbers

Use the iteration variable to set the starting value (Start#), then gradually execute a group of values in the range until the value exceeds the set end value (End#). /L will perform the iteration variable by comparing Start# with End#. If Start# is less than End#, the command will be executed. If the iteration variable exceeds End#, the command interpreter exits this loop. You can also use a negative Step# to gradually execute the values in this range in a decreasing numerical way. For example, (1,1,5) generates the sequence 1 2 3 4 5, and (5,-1,1) generates the sequence (5 4 3 2 1). The syntax is:

System help format: for /L %% Variable in (Start#,Step#,End#) do Command

For example:

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

Save and execute to see the effect, it will print the numbers from 1 2 3 4 5 like this.

The parameter (1,1,5) means starting from 1, adding 1 each time until 5 ends!

Look at this example again

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

Did you get a shock after execution? How come there are 5 more CMD windows, hehe! If you change that (1,1,5) to (1,1,65535), what will happen? I'll tell you first, 65535 CMD windows will be opened.... If you don't crash, you are strong!

Of course, we can also change that start cmd to md %%i, and then it will create the specified number of directories... with names from 1 to 65535.

After watching this destructive parameter given by me, let's look at the last parameter

/f

Detailed explanation of FOR with /F

The FOR with /F has great use and is used the most in batch processing. The usage is as follows:

Format:

FOR /F ["options"] %%i IN (file) DO command

FOR /F ["options"] %%i IN ("string") DO command

FOR /F ["options"] %%i IN ('command') DO command

This is probably the most commonly used and most powerful command, mainly used to process the output results of files and some commands.

file represents one or more files

string represents a string

command represents a command

["options"] is optional

For FOR /F %%i IN (file) DO command

file is the file name. According to the official statement, for will open the file in file one by one and read each file into memory before proceeding to the next file, divide it into elements one by one according to each line, and ignore blank lines. Let's take an example.

Suppose there is the following content in file a.txt:

First line, first column First line, second column First line, third column

Second line, first column Second line, second column Second line, third column

Third line, first column Third line, second column Third line, third column

What command would you use to display the content in a.txt? Of course it is type, type a.txt

for can also complete the same command:

for /f %%i in (a.txt) do echo %%i

Still start from the parentheses execution, because there is the parameter /f, so for will first open a.txt, then read all the content in a.txt, treat it as a set, and take each line as an element, so this set will be generated,

{"First line, first column First line, second column First line, third column", // first element

"Second line, first column Second line, second column Second line, third column", // second element

"Third line, first column Third line, second column Third line, third column"} // third element

There are only 3 elements in the set, and %%i is used to replace each element in turn, and then the command after do is executed.

Specific process:

Replace %%i with "First line, first column First line, second column First line, third column", execute the echo %%i after do, and display "First line, first column First line, second column First line, third column",

Replace %%i with "Second line, first column Second line, second column Second line, third column", execute echo %%i, and display "Second line, first column Second line, second column Second line, third column",

And so on until each element is replaced.

To strengthen the understanding of the role of /f, please execute the following two commands and compare to understand:

for /f %%i in (a.txt) do echo %%i // This will display the content in a.txt because of the role of /f, which will read the content in a.txt.

for %%i in (a.txt) do echo %%i // And this will only display the name a.txt and will not read its content.

Through the above study, we find that for /f will default to taking each line as an element, but if we also want to decompose each line into smaller content, what should we do? Don't worry, the for command also provides us with more detailed parameters, which makes it possible for us to decompose each line into smaller elements.

They are: delims and tokens

delims is used to tell for what to use as the delimiter for each line. The default delimiters are spaces and tab keys.

For example, for the above file, we execute the following command:

for /f "delims= " %%i in (a.txt) do echo %%i

The result displayed is:

First line, first column

Second line, first column

Third line, first column

Why is this? Because there is the delims parameter here, and there is a space after =, which means to split each element with a space again, and it defaults to only taking the first element after splitting.

The execution process is:

Split the first element "First line, first column First line, second column First line, third column" into three elements: "First line, first column" "First line, second column" "First line, third column", it defaults to only taking the first one, that is, "First line, first column", then execute the command after do, and so on.

But this is still limited. If we want the second column element of each line, how can we do it?

At this time, tokens comes out and says that I can do it.

Its role is when you divide each line into smaller elements through delims, it controls which one or several to take.

Still in the above example, execute the following command:

for /f "tokens=2 delims= " %%i in (a.txt) do echo %%i

Execution result:

First line, second column

Second line, second column

Third line, second column

If you want to display the third column, just change it to tokens=3.

At the same time, tokens supports wildcards *, as well as limited ranges.

If you want to display the second column and the third column, then change it to tokens=2,3 or tokens=2-3, and if there are more, it is tokens=2-10 and so on.

The command at this time is:

for /f "tokens=2,3 delims= " %%i in (a.txt) do echo %%i %%j

Why is there an extra %%j?

This is because you need to take two columns of each line behind tokens, use %%i to replace the second column, and use %%j to replace the third column.

And it must be arranged in alphabetical order, %%j cannot be replaced with %%k, because i is followed by j.

The execution result is:

First line, second column First line, third column

Second line, second column Second line, third column

Third line, second column Third line, third column

For the wildcard *, it is to treat this line as a whole or the remaining part of this line as an element.

For example:

for /f "tokens=* delims= " %%i in (a.txt) do echo %%i

The execution result is:

First line, first column First line, second column First line, third column

Second line, first column Second line, second column Second line, third column

Third line, first column Third line, second column Third line, third column

In fact, it is the same as the execution result of for /f %%i in (a.txt) do echo %%i.

Another example:

for /f "tokens=2,* delims= " %%i in (a.txt) do echo %%i %%j

The execution result is:

First line, second column First line, third column

Second line, second column Second line, third column

Third line, second column Third line, third column

Replace %%i with the second column and %%j with the remaining all.

Finally, there are skip and eol, these two are simple. skip is to ignore the first few lines of the file, and eol is used to specify that if a line starts with what symbol, it will be ignored.

For example:

for /f "skip=2 tokens=*" %%i in (a.txt) do echo %%i

The result is:

Third line, first column Third line, second column Third line, third column

Use skip to tell for to skip the first two lines.

If you don't add tokens=* in front, the execution result is:

Third line, first column

I don't know why.

Again, when the content of a.txt becomes:

. First line, first column First line, second column First line, third column

. Second line, first column Second line, second column Second line, third column

Third line, first column Third line, second column Third line, third column

Execute for /f "eol=. tokens=*" %%i in (a.txt) do echo %%i, and the result is:

Third line, first column Third line, second column Third line, third column

Use eol to tell for to ignore the lines starting with ".".

Also, tokens=* must be added, otherwise only "Third line, first column" will be displayed.
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
zhxy9804 +2 2008-10-05 11:45
740011611 +1 2009-12-07 20:27
Floor 2 Posted 2008-08-03 11:05 ·  中国 黑龙江 牡丹江 电信
新手上路
Credits 8
Posts 7
Joined 2008-07-20 22:52
18-year member
UID 121795
Gender Male
From 黑龙江
Status Offline
I didn't understand /f before. After reading what you wrote, I get it. It's really very detailed.
Floor 3 Posted 2008-08-05 23:00 ·  中国 广东 广州 移动
初级用户
Credits 116
Posts 42
Joined 2007-07-29 17:20
19-year member
UID 94255
Gender Male
Status Offline
The explanation of /f is really very good and detailed~~~ For people like me who know a little but not all, it's the best thing ever···
Floor 4 Posted 2008-08-06 09:03 ·  中国 河南 南阳 联通
初级用户
Credits 91
Posts 45
Joined 2007-03-14 07:04
19-year member
UID 81689
Gender Male
Status Offline
I found the for I looked for before, and it seems I didn't see the /d parameter. I learned something more today.
Floor 5 Posted 2008-08-19 23:02 ·  中国 江苏 盐城 建湖县 电信
初级用户
Credits 53
Posts 27
Joined 2007-12-22 21:39
18-year member
UID 106500
Gender Male
Status Offline
In the cmd, entering `for /?` is also very detailed, but still thank the landlord, you've worked hard
Floor 6 Posted 2008-08-22 10:49 ·  中国 上海 图像数据通信有限公司
新手上路
Credits 14
Posts 6
Joined 2008-08-18 17:05
17-year member
UID 123509
Gender Male
Status Offline
I encountered a problem under DOS 6.22. The FOR command seems unable to add parameters. Has anyone else come across this situation? What could be the reason?
Floor 7 Posted 2008-09-11 19:09 ·  中国 山东 临沂 兰山区 电信
中级用户
★★
Credits 208
Posts 97
Joined 2006-12-28 22:08
19-year member
UID 74845
Gender Male
Status Offline
I'm extremely grateful. This time I finally understand it, thank you so much!
Floor 8 Posted 2008-09-28 23:21 ·  中国 浙江 台州 电信
新手上路
Credits 3
Posts 2
Joined 2008-09-24 21:54
17-year member
UID 126537
Gender Male
Status Offline
Absolutely a great article!It has untied a knot in my heart.
Floor 9 Posted 2008-10-05 23:40 ·  中国 广东 东莞 电信
初级用户
Credits 61
Posts 39
Joined 2007-03-22 16:49
19-year member
UID 82575
Gender Male
From come from hb--wh
Status Offline
Very detailed explanation of the For command...!!! Seen the most detailed explanation. Hehe... Give it a thumbs up...~~~~
Floor 10 Posted 2008-10-29 12:33 ·  中国 广西 梧州 电信
新手上路
Credits 7
Posts 8
Joined 2008-08-30 08:57
17-year member
UID 124547
Gender Male
Status Offline
I finally understood~~ I read it several times and it's so touching!!
Floor 11 Posted 2008-11-07 16:07 ·  中国 湖北 武汉 武昌区 电信
新手上路
Credits 2
Posts 1
Joined 2008-11-06 15:47
17-year member
UID 130174
Gender Male
From 武汉
Status Offline
for /f "tokens=* delims= " %%i in (a.txt) do echo %%i

The execution result is:

Column 1 of row 1 Column 2 of row 1 Column 3 of row 1
Column 1 of row 2 Column 2 of row 2 Column 3 of row 2
Column 1 of row 3 Column 2 of row 3 Column 3 of row 3

In fact, it is the same as the execution result of for /f %%i in (a.txt) do echo %%i.

These two syntaxes have different execution results. The result of the second syntax only displays Column 1 of row 1, Column 1 of row 2, Column 1 of row 3!
It is related to what you later said "I don't know what's going on." I actually don't know what the problem is either. I hope I can help you a little.
Floor 12 Posted 2008-11-07 20:03 ·  中国 湖北 鄂州 电信
新手上路
Credits 4
Posts 2
Joined 2008-11-06 12:50
17-year member
UID 130160
Gender Male
Status Offline
I didn't have as detailed as yours when I first learned FOR
Floor 13 Posted 2008-11-07 22:50 ·  中国 湖南 长沙 电信
新手上路
Credits 18
Posts 17
Joined 2008-11-07 22:13
17-year member
UID 130325
Gender Male
Status Offline
Saved,,So powerful, finally understood for /f
Floor 14 Posted 2008-11-10 14:45 ·  中国 贵州 遵义 移动
新手上路
Credits 6
Posts 2
Joined 2006-11-21 21:54
19-year member
UID 71279
Gender Male
Status Offline
Hehe After reading this article, I almost understand the for statement. Thanks
Floor 15 Posted 2008-11-18 00:44 ·  中国 福建 泉州 安溪县 电信
新手上路
Credits 2
Posts 1
Joined 2008-11-09 14:17
17-year member
UID 130456
Gender Male
Status Offline
The command in this line:
Column 2 of line 1
Column 2 of line 2
Column 2 of line 3
Forum Jump: