【问题标题】:Wait for a .bat file to close within a windows batch file等待 .bat 文件在 Windows 批处理文件中关闭
【发布时间】:2016-05-26 11:13:11
【问题描述】:

我需要创建一个 Windows 批处理文件 (*.bat) 文件,该文件仅在某些进程(和批处理文件)未运行时才运行其命令。

我在这里查看了适用于进程 (*.exe) 的解决方案: How to wait for a process to terminate to execute another process in batch file

我想做一些非常相似的事情,但是有一个困难:批处理文件在“TASKLIST”命令中显示为“cmd.exe”。

我想检查一个特定的bat文件是否正在运行,例如:“C:\mybatch.bat”,如果是,请等到它关闭。

【问题讨论】:

  • tasklist /v 给出窗口标题。就好像它是窗口标题 - cmd 不会打扰更改实际标题。

标签: windows batch-file


【解决方案1】:

检查特定的 bat 文件 mybatch.bat 是否正在运行可能是比乍一看更艰巨的任务。

tasklist /V 中查找特定窗口标题以及在wmic process where "name='cmd.exe'" get CommandLine 中测试CommandLine 属性可能会在某些可以想象的情况下失败

第一。可以吗

  • mybatch.bat 开头添加title ThisIsDistinguishingString 命令
  • mybatch.bat 中删除所有其他title 命令
  • 确保mybatch.bat调用另一个包含title 命令的批处理脚本?

然后检查从find command返回的errorlevel如下:

:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
    rem echo mybatch.bat batch not found
) else (
    echo mybatch.bat is running %date% %time%
    timeout /T 10 /NOBREAK >NUL 2>&1
    goto :testMybatch
)

第二。否则,检查wmic Windows Management Instrumentation command 输出是否有帮助

wmic process where "name='cmd.exe'" get /value

然后你可以在其输出中检测到mybatch.bat,缩小为

wmic process where "name='cmd.exe'" get CommandLine, ProcessID

注意wmic 可能会返回一些Win32_Process class 属性,尤其是CommandLine,如果特定进程在另一个用户帐户下启动或提升(以管理员身份运行),则
提升的wmic 完整返回所有属性。

【讨论】:

    【解决方案2】:

    你所说的都是默认发生的。

    为了测试,创建一个新的 .bat 文件(比如说 1.bat)并放入其中

    计算

    mspaint

    保存并运行它。 计算器将启动。您会注意到只有在您关闭计算器时才会启动 Paitbrush。

    【讨论】:

    • 你误解了我的问题。我需要检查的批处理文件是否在我需要编写的批处理文件之外/之前运行。
    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多