
Ideally the targeted application should already mutex itself to prevent multiple instances, but that is a topic for another SO question and is not necessarily applicable to the subject of this question. Should you want one anyway, either a GOTO or a flag variable is indicated. There is no guarantee that there be only a single instance running when you run this script.

Keep in mind that if you add an ELSE clause then it will be executed once for every instance of the application that is already running.

The only concern is whether or not the process we are targeting has space(s). The original code will work fine whether or not other running processes have spaces in their names. If you are concerned that the program name may have spaces in it then you need to complicate the code very slightly: SETLOCAL EnableExtensionsįOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF NOT %%x = %EXE_% ( The original code apparently had a GOTO in it, which someone in the comments thought uncouth.

SETLOCAL EnableExtensionsįOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF NOT %%x = %EXE% ( At the end I could find a fairly simple solution, so I share it. So I added the idea from Matt Lacey's solution, which creates an also avoidable temp file. I like Chaosmaster's solution! But I looked for a solution which does not start another external program (like find.exe or findstr.exe).
