Search code examples
batch-filetasklist

Batch: Checking if process is active not working


this feels like a stupid issue but I can't solve it somehow. I want my batch file to check if a specific process is running. I've used an example from this page and it's working sometimes:

@echo off
tasklist /FI "IMAGENAME eq My Auto Updater.exe" 2>NUL | find /I /N "My Auto Updater.exe" >NUL
if "%ERRORLEVEL%"=="0" echo Program is running
PAUSE

This works as expected and tells me if "My Auto Updater.exe" is running. However, I noticed that this exact same code won't work for other Exe files, a random example where it fails to detect if the process is running:

@echo off
tasklist /FI "IMAGENAME eq Office 15 Click-to-Run Localization Component.exe" 2>NUL | find /I /N "Office 15 Click-to-Run Localization Component.exe" >NUL
if "%ERRORLEVEL%"=="0" echo Program is running
PAUSE

I can't figure out why this fails with the example "Office 15 Click-to-Run Localization Component.exe". Could it have something to do with the numbers in the exe name? I appreciate every answer!


Solution

  • Essentially your find is looking for specifically "office 15 click-to-run localization component.exe" in a document you pipe in from the tasklist, but the tasklist cuts the name of the process short. Because the find is so exact it doesn't find what you are looking for.

    Add /fo csv to your tasklist command and it will return longer names, then the find will find it when its piped in.

    tasklist /FO csv /FI "IMAGENAME eq Office 15 Click-to-Run Localization Component.exe" 2>NUL | find /I /N "Office 15 Click-to-Run Localization Component.exe" >NUL
    if "%ERRORLEVEL%"=="0" echo Program is running
    PAUSE