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-06-24 16:33
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] FOR Command-Line Parsing Mechanism DigestI View 12,954 Replies 26
Original Poster Posted 2006-06-22 19:11 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
To All:

FOR's command-line parsing mechanism is very complicated, with many details that can be discussed. What is discussed here first is the parsing mechanism for the traversal set that easily raises doubts (that is, the part in parentheses after in and before do).

Before that, first bring up a practical application problem . Strictly speaking, this cannot be counted as a BUG in FOR, but rather an improper continuation of an old syntax feature.

Regarding FOR's old syntax features, there was discussion in . I have now slightly整理ed and supplemented it:

1. The content in () after the IN statement is called FOR's traversal set. It can contain multiple elements, with each element separated by parameter separators such as spaces, semicolons, equals signs, etc. FOR will process all elements one by one according to the following process.

2. If an element contains filename wildcards (asterisk or question mark), then FOR interprets this element as a filename. It will look in the current path, or the path specified in the element, for files that can match. If found, it replaces the FOR variable with their filenames one by one and executes the statement after DO; if not found, it does not execute the statement after DO, and gives no prompt.

3. If an element does not contain filename wildcards, then FOR interprets it as an ordinary string, replaces the FOR variable, and executes the statement after DO.

4. During filename substitution, if a path is specified in the element, then the substituted FOR variable will also contain the same path, and vice versa.

5. In MSDOS6 and earlier versions, long filenames are not supported, so filenames enclosed in quotes are also not supported. Therefore, when quotes and wildcards are used at the same time in a traversal element, there will be no execution result.

In short, if there are wildcards, filename substitution is performed; otherwise, string substitution is performed.

In the later MSDOS7.10/COMMAND@Win9x/CMD@WinNT, this feature was retained, with only recognition and parsing of quoted filenames added on top of it.

As for why FOR/R and FOR/D still continue this feature, this is indeed a question worth discussing, because in the vast majority of cases, our purpose in using FOR/R or FOR/D is to perform substitution of file or directory names, not ordinary text substitution.

But clearly, the design thinking of the FOR command differs from ours. Also take a look through FOR's command-line help information . At this point, we can speculate what this design thinking is: FOR/D only switches filename matching to directory-name matching; FOR/R only switches matching in a specified directory to matching in a specified directory tree. Neither of these will, nor should, affect the choice between filename and string matching. This design keeps changes to the program as small as possible; at least the code for analyzing the traversal set does not need much modification.

However, how many people know and understand this feature?

FOR/R syntax bug
http://groups.google.com/group/alt.msdos.batch.nt/browse_thread/thread/84b80fadc616f74a/

E:\>dir/s/b
File Not Found

E:\>md test

E:\>echo test>test\x.txt

E:\>for /r %f in (x.txt) do @echo %f
E:\x.txt
E:\test\x.txt

E:\>for /r %f in (x.txt*) do @echo %f
E:\test\x.txt


After running XMSDSK, under its virtual disk directory, loop FOR no longer supports the * wildcard?
http://www.cn-dos.net/forum/viewthread.php?tid=15744

FOR /? in CMD@WinXP

FOR /D %variable IN (set) DO command

If set contains wildcards, then it specifies matching against directory
names, not file names.

FOR /R path] %variable IN (set) DO command

Walks the directory tree rooted at path, pointing to a
FOR statement in each directory. If no directory is specified after /R,
the current directory is used. If set is only a single period (.)
character, then the directory tree is enumerated.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2006-06-23 23:48 ·  中国 广东 广州 天河区 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
I have seen many applications of FOR; they are not about traversing directories, but more often about using FOR to parse. So I guess this is why not many people know and understand the feature you mentioned.

[ Last edited by 220110 on 2006-6-23 at 23:52 ]
Floor 3 Posted 2006-06-24 18:19 ·  中国 上海 长宁区 电信
中级用户
★★
论坛第一菜鸟
Credits 361
Posts 166
Joined 2006-04-15 13:23
20-year member
UID 53870
Status Offline
Does FOR IN DO have to be uppercase?
Diskette Operating System
Floor 4 Posted 2007-01-17 05:34 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
Unfortunately, when I arrived, expert willsort had already left....

Personally, I feel the /R and /D parameters can completely be replaced by /F and no parameters.
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 5 Posted 2007-01-17 06:58 ·  中国 江西 景德镇 电信
初级用户
Credits 77
Posts 34
Joined 2007-01-11 04:58
19-year member
UID 76137
Gender Male
From 景德镇
Status Offline
I support the statement on the 2nd floor!!
我喜欢,我选择。。
Floor 6 Posted 2007-03-16 07:50 ·  中国 湖北 武汉 江夏区 电信
初级用户
Credits 76
Posts 38
Joined 2007-03-13 02:54
19-year member
UID 81544
Gender Male
Status Offline
Support
Floor 7 Posted 2007-03-16 08:09 ·  中国 广东 广州 番禺区 电信
中级用户
★★
脚本爱好者
Credits 238
Posts 93
Joined 2007-03-11 13:38
19-year member
UID 81417
Gender Male
From GZ
Status Offline
for in do has such powerful features. I hope the batch file experts will talk more about applications in this area, so that we batch file newbies can benefit from it. Many thanks.
Floor 8 Posted 2007-03-18 03:40 ·  中国 上海 黄浦区 电信
初级用户
Credits 49
Posts 22
Joined 2005-08-04 13:44
20-year member
UID 41332
Gender Male
Status Offline
Learning
Floor 9 Posted 2007-03-22 08:28 ·  中国 广西 梧州 电信
新手上路
Credits 4
Posts 2
Joined 2007-01-09 08:32
19-year member
UID 75933
Gender Male
Status Offline
Thanks to the OP for sharing
Floor 10 Posted 2007-03-28 22:07 ·  中国 湖南 长沙 联通
社区乞丐
★★
gaoshou
Credits -16
Posts 77
Joined 2007-01-30 09:11
19-year member
UID 78105
Gender Male
Status Offline
Thanks
:cool:
Floor 11 Posted 2007-03-29 05:03 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
Really powerful, I must learn how to use it
Floor 12 Posted 2007-03-29 05:56 ·  中国 上海 电信
初级用户
Credits 45
Posts 21
Joined 2007-03-14 23:10
19-year member
UID 81739
Gender Male
Status Offline
Learning from it

Master, you are really amazing!
Floor 13 Posted 2007-04-20 05:37 ·  中国 辽宁 大连 联通
初级用户
Credits 70
Posts 38
Joined 2007-03-24 09:25
19-year member
UID 82762
Gender Male
Status Offline
Study, study, and study again~~~
Floor 14 Posted 2007-05-17 15:49 ·  中国 广东 深圳 联通
新手上路
Credits 16
Posts 7
Joined 2006-09-29 02:27
19-year member
UID 63994
Gender Male
From 河南省南阳市
Status Offline
Copied it to study.. Good stuff..
Floor 15 Posted 2007-05-17 22:44 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 45
Joined 2007-03-14 07:04
19-year member
UID 81689
Gender Male
Status Offline
After reading it I'm a bit confused..
Forum Jump: