Search code examples
batch-filevbscriptbatch-processing

Check if a process is running or not?


I want to check if a process is running or not ? if the process is not running, then i execute it (in this example I took the calculator with process name = calc.exe) I started the batch script, but I have a syntax problem I believe !

@echo off
Set MyProcess=calc.exe
echo %MyProcess%
pause
for /f "tokens=1" %%i In ('tasklist /NH /FI "imagename eq %MyProcess%"') do set ff=%%i
echo %ff%
If /i %ff%==%MyProcess% (Echo %ff% est en cours d^'execution) Else (Start %MyProcess%)
pause

Solution

  • Here's another way to do it using your code as a base:

    @echo off
    Set "MyProcess=calc.exe"
    echo "%MyProcess%"
    tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
    If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyProcess%")
    pause