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-12 06:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can wildcards be used in for /f? View 1,468 Replies 12
Original Poster Posted 2007-03-18 05:19 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
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 ]
test
Floor 2 Posted 2007-03-18 12:06 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

for /r .\ %a in (*.txt) do @echo %a
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
tianlijian +2 2007-03-20 05:25
Floor 3 Posted 2007-03-18 22:52 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Brother lxmxn used it. The official explanation seems to be enumerating the current directory
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 4 Posted 2007-03-19 04:19 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
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.
test
Floor 5 Posted 2007-03-19 05:40 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by tianlijian at 2007-3-18 15:19:
@FOR /F "usebackq delims= tokens=*"  %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i

Why is the ^ used before the |?


  The for command specifies that the redirection operators (> \ >> \ < \ |) in the parentheses after in need to be escaped, that is, a ^ is added in front of them to achieve escaping, otherwise an error will occur.
Floor 6 Posted 2007-03-19 21:45 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
@FOR /F "tokens=*" %i IN ('dir *.txt') DO @ECHO %i
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
tianlijian +2 2007-03-20 05:24
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 7 Posted 2007-03-20 05:16 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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 ...
我今后在论坛的目标就是做个超级坏人!!!
Floor 8 Posted 2007-04-07 11:36 ·  中国 天津 电信
初级用户
Credits 58
Posts 25
Joined 2006-11-29 13:11
19-year member
UID 72096
Gender Male
Status Offline
.```
for /r. %a in (*.txt) do @echo %a
```

Can the `\` after `.` be omitted?
Floor 9 Posted 2007-04-07 12:41 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by logictianjin at 2007-4-6 22:36:
for /r . \
Can the backslash after . be omitted?


Why not just try it?
Floor 10 Posted 2007-04-10 20:33 ·  中国 天津 电信
初级用户
Credits 58
Posts 25
Joined 2006-11-29 13:11
19-year member
UID 72096
Gender Male
Status Offline
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 ]
Floor 11 Posted 2007-06-14 13:57 ·  中国 湖北 潜江 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
I always can't fully understand the for / f.
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 12 Posted 2007-06-15 14:33 ·  中国 上海 徐汇区 电信
新手上路
Credits 4
Posts 2
Joined 2007-06-14 17:17
19-year member
UID 91324
Gender Male
Status Offline
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
Floor 13 Posted 2009-08-06 23:40 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
. \ means the current directory and all its subdirectories, for example, dir .\ and you can see it.
Forum Jump: