中国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-12 06:07
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Can wildcards be used in for /f?
Printable Version  1,466 / 12
Floor1 tianlijian Posted 2007-03-18 05:19
初级用户 Posts 45 Credits 120
I want to display all current .txt files
@FOR /F "usebackq delims= tokens=*" %i IN (*.txt) DO @ECHO %i

This is wrong because the asterisk in the parentheses after in in for /f seems not to be able to be used as a wildcard. It is okay in for /d and for /r. I don't know if my understanding is correct?

So I adopted the following method
@FOR /F "usebackq delims= tokens=*" %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i

This works, but there is a problem that files like a.txt.mp3 will also be displayed. Is there a better solution?

[ Last edited by tianlijian on 2007-3-18 at 03:18 PM ]
Floor2 lxmxn Posted 2007-03-18 12:06
版主 Posts 4,938 Credits 11,386
Floor3 oilio Posted 2007-03-18 22:52
高级用户 Posts 303 Credits 641
Brother lxmxn used it. The official explanation seems to be enumerating the current directory
Floor4 tianlijian Posted 2007-03-19 04:19
初级用户 Posts 45 Credits 120
In a Windows batch file, when you are using the `dir` command inside a `for /f` loop and you want to pass the `dir` command's output to `find`, the `|` character is a special character in the command line. To escape it so that it is passed correctly to the `for /f` loop's command execution, you need to use the caret (`^`) to escape it. So the `^|` is used to escape the pipe character so that it is treated as a literal character when the command is parsed within the `for /f` construct.

The original Chinese text's question is about why the `^` is used before the `|` in the given code. The translated explanation as above explains the reason for using `^` to escape the pipe character in the batch file context.
```code
@FOR /F "usebackq delims= tokens=*" %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i
```
Why is the `^` used before the `|`? In a Windows batch file, when using the `dir` command within a `for /f` loop and redirecting the output with `|` to `find`, the `|` is a special character. To properly pass it through, the `^` is used to escape the `|` so that it is interpreted as a literal pipe character during command parsing within the `for /f` construct.
Floor5 lxmxn Posted 2007-03-19 05:40
版主 Posts 4,938 Credits 11,386
Originally posted by tianlijian at 2007-3-18 15:19:
Floor6 Climbing Posted 2007-03-19 21:45
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
@FOR /F "tokens=*" %i IN ('dir *.txt') DO @ECHO %i
Floor7 9527 Posted 2007-03-20 05:16
银牌会员 Posts 438 Credits 1,185 From 北京
I understand the FOR statement as follows:
FOR /F only operates on strings, commands, and single files,
for /f .....in (" ") do ...
for /f .....in (file name) do ...
for /f .....in ('command') do ...
Floor8 logictianjin Posted 2007-04-07 11:36
初级用户 Posts 25 Credits 58
.```
for /r. %a in (*.txt) do @echo %a
```

Can the `\` after `.` be omitted?
Floor9 lxmxn Posted 2007-04-07 12:41
版主 Posts 4,938 Credits 11,386
Originally posted by logictianjin at 2007-4-6 22:36:
for /r . \
Can the backslash after . be omitted?


Why not just try it?
Floor10 logictianjin Posted 2007-04-10 20:33
初级用户 Posts 25 Credits 58
Originally posted by lxmxn at 2007-4-6 11:41 PM:


Why not just try it?


I think if you use for /r and the set contains wildcards, you can completely not use .\. If there are no wildcards in the set, it seems you need to add a dot. I don't know if my understanding is correct. I'm a beginner, please give me some advice.

[ Last edited by logictianjin on 2007-4-10 at 07:56 AM ]
Floor11 6622186 Posted 2007-06-14 13:57
高级用户 Posts 411 Credits 894
I always can't fully understand the for / f.
Floor12 erpangpang Posted 2007-06-15 14:33
新手上路 Posts 2 Credits 4
I think for /r .\
This is probably the same as for /r . and both mean all subdirectories under this directory...
for /r \ means all directories under this drive
I don't know if my understanding is correct, please give guidance
Floor13 HUNRYBECKY Posted 2009-08-06 23:40
银牌会员 Posts 442 Credits 1,179
. \ means the current directory and all its subdirectories, for example, dir .\ and you can see it.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023