中国DOS联盟

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

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

中国DOS联盟论坛
现在时间是 2026-06-28 14:10
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 哪位指导我一下如何用for 查看 664 回复 2
楼 主 哪位指导我一下如何用for 发表于 2008-04-10 15:18 ·  中国 江苏 苏州 电信
新手上路
积分 14
发帖 6
注册 2008-04-10 14:37
18年会员
UID 115476
性别 男
状态 离线
想写个这样的批处理
加入5台电脑ip分别为
192.168.0.2
192.168.0.9
192.168.0.10
192.168.0.20
192.168.0.41
不是连续的ip地址
#---------------------------------------------------------------
@echo off
ping 192.168.0.2|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!

ping 192.168.0.9|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!

ping 192.168.0.10|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!

ping 192.168.0.20|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!

ping 192.168.0.41|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!
#----------------------------------------------------------------

5台电脑可以这样,如果有上几十台上百台,这样写就比较笨了
我就尝试用for语句来实现
把那5个ip分行写道iplist.txt
然后
#----------------------------------------------------------------
@Echo off
for /F %%i in (iplist.txt) do (
ping %%i|find "Reply"
if not errorlevel 1 net send 192.168.0.1 I'm Down!
)
#----------------------------------------------------------------
因为对for不是很熟,我是想当然写的,所以运行明显有异常
哪位帮我改改
2 发表于 2008-04-10 15:31 ·  IANA 局域网IP(Private-Use)
初级用户
积分 70
发帖 30
注册 2008-01-17 14:22
18年会员
UID 108844
性别 男
状态 离线
需要启用变量延迟的

这样写

@Echo off&setlocal EnableDelayedExpansion
for /F %%i in (iplist.txt) do (
ping %%i|find "Reply"
if not errorlevel 1 net send %%i I'm Down!
)

感觉你写反了,你应该是记录没有开机的电脑吧,那么你应该写if errorlevel 1 net sent...
3 发表于 2008-04-10 16:38 ·  中国 江苏 苏州 电信
新手上路
积分 14
发帖 6
注册 2008-04-10 14:37
18年会员
UID 115476
性别 男
状态 离线
谢谢楼上的大哥
论坛跳转: