中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-13 02:26
47,811 主题排行 / 349,897 发帖 / 今日 0 篇 / 48,256 会员排行
DOS批处理 & 脚本技术(批处理室) » [求助]关于for的参数赋值
可打印版本  965 / 1
第1楼 fengwuxzg 发表于 2009-12-18 16:58
新手上路 发帖 11 积分 12
[求助]关于for的参数赋值
两个问题,有点长,望不吝赐教
for %%a in (*.txt) do (command)语句中
1. %%a被赋值时,是取出当前目录下文件名与*.txt进行匹配,若能匹配则将其赋予%%a吗,还是*取尽所有可能的字符(串)(毕竟文件名长度是有限的),然后再判断当前目录下是否存在*.txt文件,若存在则将其赋值给%%a(个人认为第二种情况不太可能,运算量太大)
2.(重点)赋值顺序为何?(我注意到是与dir时列表顺序相同) 在何种条件下退出循环??(似乎并不是一次遍历完当前目录中.txt文件后就退出)
对此小弟自己做了以下测试,以下是测试结果:
说明:当前目录下存在1.txt,2.txt,3.txt三个文档文件(XP系统)
测试代码:@


Press any key to continue . . .
.......................................
1.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.......................................
3.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.......................................
2.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.......................................
1.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
over-----------------------------------
Press any key to continue . . .

(不好意思,不知道怎样贴图片上来
可见1.txt被两次赋予%%a,虽然这两个1.txt确实是不同的文件,但是2.txt,3.txt应该也是一样啊,难道for是在再次取到相同的参数时就退出吗??(另外我还注意到,在多次执行时,赋值顺序是比较有规律的,1321->3213->2132……)
若此时我再新建一个文件4.txt,执行结果为
Press any key to continue . . .
.........................................
1.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.........................................
3.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.........................................
2.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.........................................
1.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
Press any key to continue . . .
.........................................
4.txt
Press any key to continue . . .
Press any key to continue . . .
GET VALUE!!!!!!!!!!!!!!!!!!!
Press any key to continue . . .
over-------------------------------------------------------
Press any key to continue . . .

又说明并不是在再次取到同名文件时退出循环的,至此小弟有些不解,望各位大大不吝赐教,三谢!!!


[ Last edited by fengwuxzg on 2009-12-19 at 17:47 ]
第2楼 fengwuxzg 发表于 2009-12-19 16:13
新手上路 发帖 11 积分 12
没人回??

是我没表达清楚吗?

那位大侠指导一下,感激涕零!!

[ Last edited by fengwuxzg on 2009-12-19 at 19:11 ]
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023