Would like to have a suggestion on how to detect (preferably in such a way a batch file could use the result) whether a certain application is running so that some action could be taken. (Of course, not something GUI-based).
12 Answers
Thetasklist commandcan search for a given process.
Checking for success inside a batch file is a bit complicated, since
this command does not set the errorlevel.
You need then to use it in conjunction with another utility that does set it, for example thefind commandlike this:
echo off
tasklist /fi "imagename eq notepad.exe" | find "notepad.exe" > nul
if errorlevel 0 echo I found notepad.exeThe idea here is that notepad.exe will appear in the output only if
the task is found, hence find will set the errorlevel to 0 for foundand 1 for not found.
See also:
@echo off
set "_results=Not Running"
set "_imagename=executable_with_imagename_length_greater_than_25_characters.exe"
tasklist.exe /svc /fo list | find.exe /i "%_imagename%" >nul && set "_results=%_results:Not =%"
echo=%_results%Use:
Tasklist /svc /fo listUse:
Find "string" && [if/because return 0 do()] || [if/because return non 0 do()]
Notice something in this scenario
I have one executable running
The name +(.extension) of my executable has more them 25 characters in length
Using
TaskList/fi "imagename eq some_imagename.exe", has a 25 character limitation whereimagename.exewill appearUsing
TaskList/fi "imagename eq some_imagename.exe"|find "some_imagename.exe"returnerrorlevel == 0But using the command below, I always get
errorlevel == 1, due to the limitation of characters in theimagenamefield
To obtain accurate information, not limited to character length, replace:
tasklist /fi "imagename eq executable_with_imagename_length_greater_than_25_characters.exe"|find /i "executable_with_imagename_length_greater_than_25_characters.exe"
Tasklist /svc /fo list | find /i "executable_with_imagename_length_greater_than_25_characters.exe" - What these commands are doing
Inhibits echo on commands: @echo off
Define status to Not Running: set "_results=Not Running
Define The Program.eXtension: set "_imagename=executable.exe" TaskList: tasklist.exe /svc /fo list /SVC Displays services hosted in each process. /FO format Specifies the output format. Valid values: "TABLE", "LIST", "CSV".
Redirect TaskLis out to Find: tasklist.exe ... | find ... Handles input "%filtering%": find.exe /i "%_imagename%" Omit find output/string: >nul if command returned 0 runs: && next_cmd
Replace/Remove 'Not ' in var: set "_results=%_results:Not =%"
Echoes the current condition: echo=%_results%- This is all intended to explain that the use of TaskList with the flag
"/fi "imagename eq executable"is not accurate/reliable, without considering the character string containing the name.eXtension of file/program:
@echo off
set "_results=Not Running"
set "_imagename=executable.exe"
tasklist.exe /svc /fo list|find.exe /i "%_imagename%">nul && set "_results=%_results:Not =%"
echo=%_results%- A simple alternative would be:
@echo off
tasklist.exe /svc /fo list | ( find.exe /i "Program_Name.exe" ) >nul && echo;True || echo;False- What I would use:
@echo off
for /f %%i in ('"tasklist.exe /svc /fo list|find.exe /i "ProgramName.Exe">nul && echo;True||echo;False"
')do set "_bool=%%~i"
echo/%_bool%preferably in such a way a batch file could use the result
- Using argument's:
file.cmd program.exe outvar@echo off
if "%~1" == "" (set "_default=My_Default_Program.exe") else set "_default=%~nx1"
for /f %%i in ('"tasklist.exe /svc /fo list|find.exe /i "%_default%">nul && echo;True||echo;False"
')do set "_bool=%%~i" && set "%~2=%%~i"
echo/%_bool%- :: Or using
:lable....
@echo off
for /f %%i in ('"tasklist.exe /svc /fo list|find.exe /i "%~1">nul && echo;True||echo;False"
')do if "%%~i" == "True" goto :True_Label_Job
echo/%~nx1 False
rem :: command for False condition here...
rem :: command for False condition here...
rem :: command for False condition here...
exit /b || goto :eof
:True_Label_Job
echo/%~nx1 True
rem :: command for True condition here...
rem :: command for True condition here...
rem :: command for True condition here...
exit /b || goto :eofAdditional Resources:
Set /?Echo /?Find /?For /?For /F /?Tasklist /?- Redirection
|,<,>,2>, etc.
- Conditional Execution
Goto :Label|Call :Label- Variable and Substring Manipulation
- Command line parameters in batch file
- How can I pass arguments to a batch file
- How does the Windows Command Interpreter [
cmd.exe] Parse Scripts
I need to check with a bat file if a program is running in tasksheduler.
The big problem is this program is from external guys and have four
"."
in the middle of the name.I'm using this code:
tasklist /fi "IMAGENAME eq Fls.Core.Portal.UI.Shell.exe" 2>NUL | find /I /N "Fls.Core.Portal.UI.Shell.exe">NUL msg * %ERRORLEVEL% goto Exitwith program running behind I was expecting the return of
errorlevel 0, but it is returning1I believe the problem is this
"."in the name of the program.