Mad Dire
Mad Dire

Reputation: 33

How do I use the if statement with WMIC

this is my first post. I am using a batch command to check if an instance of my program is already running based on its command line argument. I am able to see if it is running by using the following code:

wmic Path win32_process Where "CommandLine Like '%Account A%' AND Caption Like '%Leads Manager.exe%'"

If the process exists, I will get a full break down of the process information, but if the process isn't running i get No Instance(s) Available.

I want to be able to run an If statement, so if the result shows No Instance(s) Available. I want to run a command without displaying the process information and if it does exists, I want to echo Account A is already running

Hopefully someone with more experience can point me in the right direction

Upvotes: 2

Views: 832

Answers (1)

Compo
Compo

Reputation: 38653

Here's a example, which may help you out:

@Echo Off
For %%A In (A B C D) Do WMIC Process Where^
 "Name='Leads Manager.exe' And Not CommandLine Like '%%Account %A%%%'"^
 Get Name 2>Nul|Find "Name">Nul && (
        Start "" "C:\Leads Manager.exe" -- "C:\Account %%A.ini")

This example uses the outer For loop to choose the Account letters, it is therfore checking to see if Leads Manager.exe is running with a commandline containing the string Account A, Account B, Account C or Account D, and if not it should run Leads Manager specifying the path to the appropriate account. (Please note that your .exe and .ini file paths both contain spaces, so I have doublequoted them, but left the rest of the command as you wrote it in your comment. You may want to consider checking that what you provided was correct and adjust it as necessary before testing, or replace that line entirely with a simple Echo Is Not Running) || Echo Is Running) statement, or perhaps run another batch file instead).

Upvotes: 2

Related Questions