1

I need to create a windows batch file (*.bat) file that only runs its commands if certain processes (and batch files) are NOT running.

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

I want to do something very similar, however, there is one difficulty: Batch files show up as "cmd.exe" in the "TASKLIST" command.

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.

zaidgs
  • 13
  • 4
  • `tasklist /v` gives the window title. It's as if it's the window title - cmd doesn't bother changing the actual title. –  May 26 '16 at 11:21

2 Answers2

0

What you say happens by default.

To test, crate a new .bat file (let's say 1.bat) and put in it

calc

mspaint

Save and run it. Calculator will start. You will notice that Paitbrush will launch only when you have closed calculator.

Overmind
  • 159
  • 10
  • You have misunderstood my question. The batch file I need to check if it running is run outside/before the batch file I need to write. – zaidgs May 28 '16 at 10:26
0

Checking if a specific bat file mybatch.bat is running could be a tougher task than it could look at first sight.

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.

1st. Can you

  • add title ThisIsDistinguishingString command at beginning of the mybatch.bat and
  • remove all other title commands from mybatch.bat and
  • ensure that mybatch.bat does not call another batch script(s) containing a title command?

Then check errorlevel returned from find command as follows:

: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. Otherwise, check if wmic Windows Management Instrumentation command output could help

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

Then you could detect mybatch.bat in its output narrowed to

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).
Elevated wmic returns all properties in full.

JosefZ
  • 28,460
  • 5
  • 44
  • 83