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/
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'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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!

DigestI