中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-13 12:38
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Discussion] FOR Command-Line Parsing Mechanism
Printable Version  14,050 / 26
Floor1 willsort Posted 2006-06-22 19:11
元老会员 Posts 1,512 Credits 4,432
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.
Floor2 220110 Posted 2006-06-23 23:48
荣誉版主 Posts 313 Credits 718
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 ]
Floor3 xjmxjm1234 Posted 2006-06-24 18:19
中级用户 Posts 166 Credits 361
Does FOR IN DO have to be uppercase?
Floor4 tghksj Posted 2007-01-17 05:34
社区乞丐 Posts 90 Credits -49
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.
Floor5 sleet1986 Posted 2007-01-17 06:58
初级用户 Posts 34 Credits 77 From 景德镇
I support the statement on the 2nd floor!!
Floor6 jackyggt Posted 2007-03-16 07:50
初级用户 Posts 38 Credits 76
Support
Floor7 axi Posted 2007-03-16 08:09
中级用户 Posts 93 Credits 238 From GZ
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.
Floor8 jianyaogao Posted 2007-03-18 03:40
初级用户 Posts 22 Credits 49
Learning
Floor9 allowtimechange Posted 2007-03-22 08:28
新手上路 Posts 2 Credits 4
Thanks to the OP for sharing
Floor10 hngaoshou Posted 2007-03-28 22:07
社区乞丐 Posts 77 Credits -16
Thanks
Floor11 stornager Posted 2007-03-29 05:03
中级用户 Posts 131 Credits 328
Really powerful, I must learn how to use it
Floor12 13579246810 Posted 2007-03-29 05:56
初级用户 Posts 21 Credits 45
Learning from it

Master, you are really amazing!
Floor13 mtiankong Posted 2007-04-20 05:37
初级用户 Posts 38 Credits 70
Study, study, and study again~~~
Floor14 luobotou Posted 2007-05-17 15:49
新手上路 Posts 7 Credits 16 From 河南省南阳市
Copied it to study.. Good stuff..
Floor15 studythedos Posted 2007-05-17 22:44
初级用户 Posts 45 Credits 91
After reading it I'm a bit confused..
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023