Board logo

标题: [系统相关] [已解决]批处理命令被跳过,没有执行? [打印本页]

作者: zhaokeistc    时间: 2015-7-2 15:33     标题: [已解决]批处理命令被跳过,没有执行?

  1. @echo off
  2. sc stop LazyStopService
  3. ping -n 12 127.0.0.1 >nul 2>nul
  4. for /f %%a in ('wmic SERVICE where name^="LazyStopService" get ProcessId^|findstr "[0-9]"') do (
  5. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  6. if %errorlevel% == 0 (
  7. echo one error
  8. sc stop LazyStopService
  9. ping -n 12 127.0.0.1 >nul 2>nul
  10. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  11. if %errorlevel% == 0 (
  12. echo two error
  13. sc stop LazyStopService
  14. ping -n 10 127.0.0.1 >nul 2>nul
  15. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  16. if %errorlevel% == 0 (
  17. echo three error
  18. taskkill /F /PID %%a
  19. )
  20. )
  21. )
  22. )
  23. echo haha
复制代码

作者: zhaokeistc    时间: 2015-7-2 15:35

LazyStopService这个服务是我特意为测试代码写的一个延时20秒停止的一个服务

根据执行结果看 第10行代码完全没有执行啊      这是什么原因
作者: DAIC    时间: 2015-7-2 16:40

把以下代码删掉:
@echo off
2>nul

再执行一次,结果发出来看看。
作者: flyinnet9    时间: 2015-7-2 16:45

本帖最后由 flyinnet9 于 2015-7-2 16:47 编辑

2,7,12,17行
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. sc stop LazyStopService
  4. ping -n 12 127.0.0.1 >nul 2>nul
  5. for /f %%a in ('wmic SERVICE where name^="LazyStopService" get ProcessId^|findstr "[0-9]"') do (
  6. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  7. if !errorlevel!== 0 (
  8. echo one error
  9. sc stop LazyStopService
  10. ping -n 12 127.0.0.1 >nul 2>nul
  11. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  12. if !errorlevel! == 0 (
  13. echo two error
  14. sc stop LazyStopService
  15. ping -n 10 127.0.0.1 >nul 2>nul
  16. tasklist /fi "pid eq %%a" |find "%%a" 2>nul
  17. if !errorlevel! == 0 (
  18. echo three error
  19. taskkill /F /PID %%a
  20. )
  21. )
  22. )
  23. )
  24. echo haha
复制代码
这样应该就可以了,不过System Idle Process服务的PID貌似是0,wmic取PID的时候,如果服务未启动,返回也是0,最好判断一下
作者: zhaokeistc    时间: 2015-7-2 17:19

回复 4# flyinnet9


    确实可以了    不过那个echo madan  我没去掉还是直接跳到echo haha了     echo是不占输出的!errorlevel!判断吗?      不应该是进入if才对吗
作者: flyinnet9    时间: 2015-7-2 17:37

!相对于%是起到变量延迟的作用,所以在前面加setlocal enabledelayedexpansion启用延迟,不启用,errorlevel只会取第一次的值,后面的判断是无效的

我眼神不好,实在是没有发现 echo madan在哪一行




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