|
tianlijian
初级用户
 
积分 120
发帖 45
注册 2007-3-13
状态 离线
|
『楼 主』:
for /f中是否可使用通配符?
使用 LLM 解释/回答一下
我想显示目下所以有的.txt文件
@FOR /F "usebackq delims= tokens=*" %i IN (*.txt) DO @ECHO %i
这个是错误在for /f的in后面的括号中*好像不能用来当作通配符。在for /d和for /r中都是可以的, 不知道我的理解对不对?
于是我采用下面的方法
@FOR /F "usebackq delims= tokens=*" %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i
这个可以,但有个问题,像a.txt.mp3这样的文件也会显示,不没有更好的解决办法。
Last edited by tianlijian on 2007-3-18 at 03:18 PM ]
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 |
|
2007-3-18 05:19 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
for /r .\ %a in (*.txt) do @echo %a
for /r .\ %a in (*.txt) do @echo %a
|
|
2007-3-18 12:06 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
lxmxn兄用到了.,官方解释好像是枚举当前目录
Brother lxmxn used it. The official explanation seems to be enumerating the current directory
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-18 22:52 |
|
|
tianlijian
初级用户
 
积分 120
发帖 45
注册 2007-3-13
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
@FOR /F "usebackq delims= tokens=*" %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i
请为一下|前面为什么要使用^?
Last edited by tianlijian on 2007-3-18 at 10:06 PM ]
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 |
|
2007-3-19 04:19 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by tianlijian at 2007-3-18 15:19:
@FOR /F "usebackq delims= tokens=*" %i IN (`dir ^| find /i ".txt"`) DO @ECHO %i
请为一下|前面为什么要使用^?
for 命令规定: in后面的括号中的命令重定向符(> \ >> \ < \ |)前需要转义,即在其前面加上一个^来实现转义,否则会出错。
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.
|
|
2007-3-19 05:40 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
@FOR /F "tokens=*" %i IN ('dir *.txt') DO @ECHO %i
@FOR /F "tokens=*" %i IN ('dir *.txt') DO @ECHO %i
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2007-3-19 21:45 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
本人理解FOR语句如下:
FOR /F 只对字符串,命令,单个文件进行操作,
for /f .....in (" ") do ...
for /f .....in (文件名称) do ...
for /f .....in ('命令') do ...
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 ...
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2007-3-20 05:16 |
|
|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-3-17 11:06 PM:
for /r .\ %a in (*.txt) do @echo %a
for /r . \
.后面的\ 是否可以省略掉?
.```
for /r. %a in (*.txt) do @echo %a
```
Can the `\` after `.` be omitted?
|
|
2007-4-7 11:36 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by logictianjin at 2007-4-6 22:36:
for /r . \
.后面的\ 是否可以省略掉?
试试不就知道啦?
Originally posted by logictianjin at 2007-4-6 22:36:
for /r . \
Can the backslash after . be omitted?
Why not just try it?
|
|
2007-4-7 12:41 |
|
|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-4-6 11:41 PM:
试试不就知道啦?
我觉得如果使用 for /r并且 集里带有通配符的话 完全可以不用 .\ 如果集中没有通配符好象就要加点了 不知道我理解的对不对,我是初学者请指教
Last edited by logictianjin on 2007-4-10 at 07:56 AM ]
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 ]
|
|
2007-4-10 20:33 |
|
|
6622186
高级用户
   
积分 894
发帖 411
注册 2007-2-17
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
for / f 总是不能完全弄明白.
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
|
|
2007-6-14 13:57 |
|
|
erpangpang
新手上路

积分 4
发帖 2
注册 2007-6-14
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
我觉得 for /r .\
这个跟for /r . 应该是一样的都表是该目录下所有子目录的。。。
for /r \这个表示该盘下所有目录
不知道我的理解是不是正确,指教
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
|
|
2007-6-15 14:33 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
.\表示当前目录及其所有子目录如dir .\你看下就知道了。
. \ means the current directory and all its subdirectories, for example, dir .\ and you can see it.
|
|
2009-8-6 23:40 |
|