0

I got this script form stackoverflow and its working for me . and because i can't comment there im asking my question in new post.

This script check if exe is running in tasklist:

@echo off
SETLOCAL EnableExtensions

set EXE=notepad.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound

goto ProcessNotFound

:ProcessFound

echo %EXE% is running
goto END
:ProcessNotFound
echo %EXE% is not running
goto END
:END
echo Finished!

The question is :

How can I check multiple process is running on tasklist?

for example exe1 and exe2

thanks in advance

10
  • (I do not believe that just outputting is or isn't running has any real purpose, so what do you really want to do?). You need to be more specific about the command(s) you wish to perform on success or failure of each process because it will make a big difference to the structure of your script. Without knowing we cannot determine whether running a single chained command or separate multiple commands would be the most efficient method. Commented Dec 30, 2017 at 12:50
  • The purpose is not just to output is or isn't running. I want to check if the process is running prevent the exe file to run again . and if not running execute the file . Commented Dec 30, 2017 at 14:11
  • Because it's trivial to start a process which isn't currently running, is your real question, "given a list of pre-defined processes, how do I prevent multiple instances of each from running?". If it is, your question needs to specify those processes because it is critical to any potential solution. Commented Dec 30, 2017 at 14:34
  • let me explain what i really want to do . I have an exe file .every time i want to open it , it must check for two process in tasklist.if one those processes are running it must break itself and if non of them are running it can continue its functionality. Commented Dec 30, 2017 at 23:13
  • 1
    I refer you to my previous comment! If you have written the non malicious .exe's yourself, why not rewrite them with the functionality you need built in? Commented Dec 31, 2017 at 13:47

3 Answers 3

1

From your comments; if all you want to do is run a third executable if neither of two other executables are running then here is a single line complete batch file example for you:

@TaskList/NH /FI "Status Eq Running"|FindStr/IC:"first.exe" /C:"second.exe">Nul||Start "" "X:\PathTo\third.exe"

Note:
Do not change anything other than the names first, second and X:\PathTo\third; all double quotes, ", are necessary!

Sign up to request clarification or add additional context in comments.

4 Comments

Exactly what i wanted , Thank you ❤️,its working well.
sorry i can't vote anybody because of my low reputation 🙏
Is it possible to find hidden processes by this way ? process is visible in windows taskmanager but its running in hidden mode and the above code does not recognize it . ( converted batch file )
You can remove the /FI "Status Eq Running" section to see if your list increases to include what you need. If it doesn't, you're going to have to provide the names and further details about your executables.
1

I've organised your code a bit differently so it's easier to follow and has more functionality. Note: This means it will be slower if you have lots of processes. If you wish to only see if it exists, I'd recommend using findstr.

I've added REM (batch-file equivalent for comments) explaining what each section does.

@echo off

REM Create variable's for exe's and their counter
set exe_1=notepad.exe
set exe_2=explorer.exe
set exe_3=chrome.exe
set "count_1=0"
set "count_2=0"
set "count_3=0"

REM Store all tasklist findings in a temp file
>tasklist.temp (
tasklist /NH /FI "IMAGENAME eq %exe_1%"
tasklist /NH /FI "IMAGENAME eq %exe_2%"
tasklist /NH /FI "IMAGENAME eq %exe_3%"
)

REM Go through all finds and count for each task instance
for /f %%x in (tasklist.temp) do (
if "%%x" EQU "%exe_1%" set /a count_1+=1
if "%%x" EQU "%exe_2%" set /a count_2+=1
if "%%x" EQU "%exe_3%" set /a count_3+=1
)

REM Use variables to see instance count
Echo %exe_1%: %count_1%
Echo %exe_2%: %count_2%
Echo %exe_3%: %count_3%

REM Use GTR 0 to see if process exists
if %count_1% GTR 0 if %count_2% GTR 0 Echo Both notepad and explorer are open

REM Delete temp file once finished. (NB: Will still exist if your code crashes)
del tasklist.temp

Conditional if-statements

As requested from your comment:

if %count_1% GTR 0 if %count_2% GTR 0 (
    Echo Both notepad and explorer are open
    goto :finish
)
if %count_1% GTR 0 (
    Echo Only notepad is open
    goto :finish
)
if %count_2% GTR 0 (
    Echo Only explorer is open
    goto :finish
)

REM Not Finished means none are open
Echo Neither notepad nore explorer are open

:finish

3 Comments

Thank you ! its work like a charm. Your code check all EXE together. could you please make it Logic conditional . for example if EXE1 OR EXE2 OR Both is running do somthing . else if non of them is running do somthing else.
Look at my edit with conditional formatting, if it suits u you can mark it as correct @MiladJafari
Thank you , your code now match my request , but see the @Compo solution . its doing all your code in just one line and i think this is the optimal way.
0

Here is my solution, keeping a similar structure to that of your sample script. Modify the EXE variable to reference the IMAGENAMEs you're interested in checking.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXE=(notepad.exe wordpad.exe winword.exe thunderbird.exe outlook.exe greenshot.exe)
FOR %%i IN %EXE% DO (
    TASKLIST /NH /FI "IMAGENAME EQ %%i" 2>NUL | FIND /I /N "%%i">NUL
    IF !ERRORLEVEL! EQU 0 ( CALL :ProcessFound %%i ) ELSE ( CALL :ProcessNotFound %%i )
)

ECHO Finished^^!
EXIT /B 0

:ProcessFound
ECHO %1 is running
EXIT /B 0

:ProcessNotFound
ECHO %1 is not running
EXIT /B 1

If you want to start the program when the process is not found, insert START %1 after ECHO %1 is not running.

6 Comments

I added two parameters (exe1.exe exe2.exe) to your code and it just check the first parameter (exe1.exe) –
Did you copy my code verbatim? I tested on Windows 7. Which O/S are you using? My only thought is that you didn't add ENABLEDELAYEDEXPANSION as per the second line of my code.
I copied your code completely from here and just changed the EXE names. I'm using windows 10
I'll have access to a Win10 machine in a few days. I'll post back with the results.
Thanks for your Follow up🙏 .but the @Compo answer did my job.that was exactly what i wanted .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.