简体   繁体   English

等待.bat文件在Windows批处理文件中关闭

[英]Wait for a .bat file to close within a windows batch file

I need to create a windows batch file (*.bat) file that only runs its commands if certain processes (and batch files) are NOT running. 我需要创建一个Windows批处理文件(* .bat)文件,该文件仅在某些进程(和批处理文件)未运行时才运行其命令。

I have looked at a solution that works for processes (*.exe) here: How to wait for a process to terminate to execute another process in batch file 我在这里查看了适用于进程(* .exe)的解决方案: 如何等待进程终止以执行批处理文件中的另一个进程

I want to do something very similar, however, there is one difficulty: Batch files show up as "cmd.exe" in the "TASKLIST" command. 我想做非常相似的事情,但是有一个困难:批处理文件在“ TASKLIST”命令中显示为“ cmd.exe”。

I want to check if a specific bat file is running, for example: "C:\\mybatch.bat", and if it is, wait until it is closed. 我想检查特定的bat文件是否正在运行,例如:“ C:\\ mybatch.bat”,如果正在运行,请等待其关闭。

What you say happens by default. 您说的是默认情况下发生的。

To test, crate a new .bat file (let's say 1.bat) and put in it 要进行测试,请创建一个新的.bat文件(例如1.bat)并放入其中

calc 计算

mspaint mspaint

Save and run it. 保存并运行它。 Calculator will start. 计算器将启动。 You will notice that Paitbrush will launch only when you have closed calculator. 您会注意到,只有在关闭计算器后,Paitbrush才会启动。

Checking if a specific bat file mybatch.bat is running could be a tougher task than it could look at first sight. 检查特定的蝙蝠文件mybatch.bat是否正在运行可能比初看起来困难得多。

Looking for a particular window title in tasklist /V as well as testing CommandLine property in wmic process where "name='cmd.exe'" get CommandLine might fail under some imaginable circumstance. tasklist /V查找特定的窗口标题,以及在wmic process where "name='cmd.exe'" get CommandLine测试“ CommandLine wmic process where "name='cmd.exe'" get CommandLine CommandLine属性,在某些可以想象的情况下,它们可能会失败

1st . 1号 Can you 你是否可以

  • add title ThisIsDistinguishingString command at beginning of the mybatch.bat and mybatch.bat开头添加title ThisIsDistinguishingString命令,
  • remove all other title commands from mybatch.bat and mybatch.bat删除所有其他title命令, 然后
  • ensure that mybatch.bat does not call another batch script(s) containing a title command? 确保mybatch.bat 不会调用另一个包含title命令的批处理脚本?

Then check errorlevel returned from find command as follows: 然后检查从find命令返回的错误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
)

2nd . 2号 Otherwise, check if wmic Windows Management Instrumentation command output could help 否则,请检查wmic Windows Management Instrumentation命令输出是否可以帮助您

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

Then you could detect mybatch.bat in its output narrowed to 然后您可以检测到mybatch.bat在其输出中缩小为

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

Note that wmic could return some Win32_Process class properties, particularly CommandLine , empty if a particular process was launched under another user account or elevated (run as administrator). 请注意,如果某个特定进程是在另一个用户帐户下启动或提升(以管理员身份运行),则wmic可能会返回一些Win32_Process属性,尤其是CommandLine ,为
Elevated wmic returns all properties in full. 高架wmic将全部返回所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM