Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jetstreamin/2530e15ee0ef863920f4 to your computer and use it in GitHub Desktop.
Save jetstreamin/2530e15ee0ef863920f4 to your computer and use it in GitHub Desktop.
Mutually exclusive applications
Source: http://www.brad-smith.info/blog/archives/535
I keep several batch scripts on my desktop that each start a set of programs; for example, I have a developer script that launches Visual Studio, SQL Management Studio and a few third-party tools. I also have a social script that launches my twitter client, instant messenger, etc. Some of these scripts overlap (i.e. the same program is included in different scripts) and, also, I may already have one or two of these applications open when I run the batch file.
In light of these requirements, I need to start the programs on my list only if they are not already running. Thankfully, this is not too difficult to do in batch scripts:
tasklist /FI "IMAGENAME eq [process name]" 2>NUL | find /I /N "[process name]">NUL
if "%ERRORLEVEL%" NEQ "0" {cmd /c} start [path to executable]
Where [process name] is the name of the process as it appears in the Windows Task Manager; e.g. firefox.exe – and [path to executable] is either the name of the executable to run (if it falls within the PATH variable) or the full path to the program you want to run. Note that the ‘cmd /c’ is optional (see the explanation below).
So, how does this work?
The tasklist command is the command-line equivalent of the Windows Task Manager. By invoking it with the /FI switch, you can apply a filter. You can filter according to a range of criteria, such as the process name, user, window title, etc – here, we just want to get an exact match (using the eq operator) on the name of the process.
The output of the tasklist command looks like this:
1
C:\Windows\system32>tasklist /FI "IMAGENAME eq firefox.exe"
2
3
Image Name PID Session Name Session# Mem Usage
4
========================= ======== ================ =========== ============
5
firefox.exe 2404 Console 1 434,096 K
By piping the output of the tasklist command into the find command, we can determine whether there was a match; by using the /I and /N switches, we perform a case-insensitive match and place the success/failure in the ERRORLEVEL variable (which is used by most command-line tools for this purpose).
We don’t want to output the results of the find command to the console, so we redirect it to NUL (i.e. nowhere).
If the find command returns a match, the ERRORLEVEL will be zero; therefore, we only need to run the program if its value is non-zero, i.e. NEQ “0”.
Running a command with cmd /c ensures that the script will go on running without waiting for the operation to complete. Some programs (e.g. Firefox) will tie up the console window if started from the command line, and we want to avoid this.
The start command is the preferred way to run GUI applications, and can also be used to open documents and URLs – we use it here because it supports the widest variety of targets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment