2016-05-26 103 views
0

我需要创建一个windows批处理文件(* .bat)文件,该文件只在某些进程(和批处理文件)没有运行时才运行它的命令。等待一个.bat文件在一个windows批处理文件中关闭

我已经看过,对于此过程工程文件(* .exe)的解决方案: How to wait for a process to terminate to execute another process in batch file

我想要做一些非常相似,然而,有一个难点:批处理文件显示为“CMD。 “TASKLIST”命令中的“exe”。

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

+0

'tasklist/v'给出窗口标题。就好像它是窗口标题 - cmd不会改变实际标题。 –

回答

0

检查一个特定的bat文件mybatch.bat是否正在运行可能是比它看起来更加棘手的任务。

寻找在tasklist /V特定的窗口标题,以及在wmic process where "name='cmd.exe'" get CommandLine测试CommandLine财产可能会失败下的一些想象的情况。

1st。你能

  • mybatch.bat
  • 删除所有其他title命令开始从mybatch.bat
  • 添加title ThisIsDistinguishingString命令确保mybatch.bat不包含调用另一个批处理脚本(S)一个title命令?

然后检查errorlevelfind command返回如下:

: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返回全部属性。

+0

谢谢。您的解决方案适合我的需求。 – zaidgs

0

你说什么默认情况下发生。

要进行测试,箱子一个新的.bat文件(假设1.BAT),并把它

MSPAINT

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

+0

您误解了我的问题。我需要检查批处理文件是否运行在我需要写入的批处理文件之外/之前运行。 – zaidgs

相关问题