1

I want to auto-launch an application (specifically Outlook.exe) if it is not already running. I wanted to do this through the Windows 7 task scheduler, which meant the solution here did not work when I tried to execute it on one line by replacing the newline with &. It would launch outlook no matter if it was running or closed.

tasklist /FI "IMAGENAME eq outlook.exe" 2>NUL | find /I /N "outlook.exe">NUL & if "%ERRORLEVEL%"=="0" "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"

Community
  • 1
  • 1
TaylorSanchez
  • 385
  • 1
  • 3
  • 8
  • As mentioned in this alternate version of the question, `tasklist.exe` can truncate program names. https://superuser.com/a/1536680/311792 – Dave Dec 10 '20 at 22:55

2 Answers2

3

Just to make a quick fix so the batch exits after successfully opening the file.

I added start ""

cmd.exe /c tasklist /FI "IMAGENAME eq outlook.exe" | find /I /N "outlook.exe" ||start "" "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"

0

I found this, which the second answer gave greater detail on the & and | options.

This is what ended up running well for me: cmd.exe /c tasklist /FI "IMAGENAME eq outlook.exe" | find /I /N "outlook.exe" || "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"

More details: || [...] command1 || command2 Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

Community
  • 1
  • 1
TaylorSanchez
  • 385
  • 1
  • 3
  • 8