Board logo

标题: [网络连接] [已解决]各位高手求助 bat脚本goto循环次数 [打印本页]

作者: smdx    时间: 2020-11-6 10:08     标题: [已解决]各位高手求助 bat脚本goto循环次数

bat脚本goto循环求助,想在ping这个步骤添加个循环次数例如30次不通就结束bat,如果ping通了就执行Connect下面的步骤。请问要怎么做啊?谢谢
  1. @echo off
  2. ::检测网络是否连接再进行下一步
  3. :Ping
  4. ping 192.168.1.1 && goto Connect || goto Ping
  5. :Connect
  6. ping 192.168.1.10 -n 1 | find "TTL"
  7. if errorlevel 1 goto Stop
  8. if errorlevel 0 goto Action
  9. exit
  10. :Stop
  11. net use * /d /y
  12. exit
  13. :Action
  14. net use * /d /y
  15. net use z: \\192.168.1.10\Video "123456" /user:123
  16. net use y: \\192.168.1.10\Music "123456" /user:123
  17. net use x: \\192.168.1.10\Driver "123456" /user:123
  18. net use w: \\192.168.1.10\Soft "123456" /user:123
  19. exit
复制代码

作者: yhcfsr    时间: 2020-11-6 10:42

  1. @echo off
  2. ::检测网络是否连接再进行下一步
  3. :Ping
  4. ping 192.168.1.1
  5. if %errorlevel% equ 0 echo;Ping 192.168.1.1 成功&&goto :Connect
  6. set /a count+=1
  7. if %count% gtr 30 goto :Stop
  8. if %errorlevel% equ 1 goto :Ping
  9. :Connect
  10. ping 192.168.1.10 -n 1 | find "TTL"
  11. if errorlevel 1 goto Stop
  12. if errorlevel 0 goto Action
  13. exit
  14. :Stop
  15. net use * /d /y
  16. exit
  17. :Action
  18. net use * /d /y
  19. net use z: \\192.168.1.10\Video "123456" /user:123
  20. net use y: \\192.168.1.10\Music "123456" /user:123
  21. net use x: \\192.168.1.10\Driver "123456" /user:123
  22. net use w: \\192.168.1.10\Soft "123456" /user:123
  23. exit
复制代码

作者: smdx    时间: 2020-11-6 12:03

yhcfsr 发表于 2020-11-6 10:42



   非常感谢高手帮忙~测试成功。
作者: Batcher    时间: 2020-11-7 16:13

回复 2# yhcfsr


    第8行的 errorlevel 应该不是第4行的命令执行之后的状态吧
作者: smdx    时间: 2020-11-7 17:07

以上命令有什么错误了吗?
作者: yhcfsr    时间: 2020-11-7 18:48

回复 4# Batcher

    我测试过:
    第6行不改变errorlevel,所以第8行还是保持第4行errorlevel




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2