中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-13 03:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [求助]关于for的参数赋值 查看 966 回复 1
楼 主 [求助]关于for的参数赋值 发表于 2009-12-18 16:58 ·  中国 湖北 武汉 教育网
新手上路
积分 12
发帖 11
注册 2009-11-28 17:39
16年会员
UID 155545
性别 男
状态 离线
两个问题,有点长,望不吝赐教
for %%a in (*.txt) do (command)语句中
1. %%a被赋值时,是取出当前目录下文件名与*.txt进行匹配,若能匹配则将其赋予%%a吗,还是*取尽所有可能的字符(串)(毕竟文件名长度是有限的),然后再判断当前目录下是否存在*.txt文件,若存在则将其赋值给%%a(个人认为第二种情况不太可能,运算量太大)
2.(重点)赋值顺序为何?(我注意到是与dir时列表顺序相同) 在何种条件下退出循环??(似乎并不是一次遍历完当前目录中.txt文件后就退出)
对此小弟自己做了以下测试,以下是测试结果:
说明:当前目录下存在1.txt,2.txt,3.txt三个文档文件(XP系统)
测试代码:@
echo off
for %%a in (*.txt) do (
pause
echo .........................................
echo %%a
type %%a>temp.txt
pause
del %%a
pause
ren temp.txt %%~na.txt
echo GET VALUE!!!!!!!!!!!!!!!!!!!
pause
)
echo over-------------------------------------------------------
pause



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 发表于 2009-12-19 16:13 ·  中国 湖北 武汉 教育网
新手上路
积分 12
发帖 11
注册 2009-11-28 17:39
16年会员
UID 155545
性别 男
状态 离线
没人回??

是我没表达清楚吗?

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

[ Last edited by fengwuxzg on 2009-12-19 at 19:11 ]
论坛跳转: