【问题标题】:Need Help to write batch file which executes another bat file if its exsists and not running需要帮助编写批处理文件,如果它存在且未运行,则执行另一个 bat 文件
【发布时间】:2013-11-04 17:21:50
【问题描述】:

假设我有一个名为 parent.bat 的 bat 文件,看起来像

@ECHO off
rem this is parent.bat file
:label
if exist child1.bat (
tasklist /v /fi "IMAGENAME eq cmd.exe"|find /I /c "hello1"
if %ERRORLEVEL% == "0" start child1.exe
)
if exist child2.bat (
tasklist /v /fi "IMAGENAME eq cmd.exe"|find /I /c "hello2"
if %ERRORLEVEL% == "0" start child2.exe
)
if exist child3.bat (
tasklist /v /fi "IMAGENAME eq cmd.exe"|find /I /c "hello3"
if %ERRORLEVEL% == "0" start child3.exe
)
timeout /t 120
goto label

我有 child1.bat

  @ECHO off
  title hello1
  start www.google.com
  timeout /t -1

我有 child2.bat

  @ECHO off
  title hello2
  start www.yahoo.com
  timeout /t -1

我有 child3.bat

  @ECHO off
  title hello3
  start www.facebook.com
  timeout /t -1

如果 child.bat 文件存在并且它们没有运行,我的意图是运行 facebook、google、yahoo

但是即使 child.bat 文件正在运行(它们处于暂停模式),parent.bat 文件也会打开所有 child.bat 文件

甚至更多时候,当我更改 %ERRORLEVEL% == "0" 中的 qoutes 时,它不会启动 child1.bat 和 child2.bat

有时相反,有时会启动 child2.bat 和 child3.bat

【问题讨论】:

    标签: windows batch-file cmd find tasklist


    【解决方案1】:
    :label
    for %%f in (1 2 3) do if exist "child%%f.bat" (
        tasklist /v /fi "IMAGENAME eq cmd.exe"|findstr /I "hello%%f" >nul 
        if errorlevel 1 start "child%%f.bat"
    )
    timeout /t 120
    goto :label
    

    【讨论】:

    • 以上代码重复打开child.bats,如果我保持标题为hello1.是否足以更改标题或我应该做其他事情
    • 抱歉,/B 参数(在行首匹配)是从测试中复制的。拿出来。
    猜你喜欢
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2013-04-25
    相关资源
    最近更新 更多