How to check (with "tasklist" built-in cmd) if prgm is running?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to check (with "tasklist" built-in cmd) if prgm is running?

#1 Post by pstein » 27 Jun 2018 01:02

How do I check (from within a DOS batch script) if a certain program (with "Imagename" is myprog.exe") is currently running?

I fiddled around with the built-in command "tasklist". But for example

tasklist /FI "Imagename eq myprog.exe"
echo el=%errorlevel%

yields always 0

I need a return code or return value to echk in a following "if" statement if the program is currently up.
Something like

tasklist /FI "Imagename eq myprog.exe"
if [%errorlevel%]==[1] ( echo "hey its running" )

If another built-in program is more suitable then its welcomed as well

Peter

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#2 Post by aGerman » 27 Jun 2018 01:39

TASKLIST does neither set the errorlevel nor does it write to StdErr. Usually I pass the output to FINDSTR which changes the errorlevel accordingly.

Code: Select all

set "proc=myprog.exe"
tasklist /nh /fi "imagename eq %proc%" | >nul findstr /bic:"%proc% "
echo %errorlevel%
Steffen

EDIT: If the process name exceeds the column width you might rather change the output format like that

Code: Select all

tasklist /nh /fo csv /fi "imagename eq %proc%" | >nul findstr /bic:"\"%proc%\","


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#4 Post by Squashman » 26 Jul 2018 15:25

Code: Select all

FOR /F "skip=1 delims=" %%G IN ('tasklist /FO CSV /FI "imagename eq notepad.exe"') do echo It is Running.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#5 Post by aGerman » 26 Jul 2018 16:02

I'm not a member on SO, Hackoo.
Are you familiar with JScript hybrids?
This is a proof of concept how to enumerate the URLs of the InternetExplorer tabs.

Code: Select all

@if (0)==(0) echo off

for /f "delims=" %%i in ('cscript //nologo //e:jscript %~fs0') do (
  echo %%i
)
pause

goto :eof @end try {
  var colShWindows = WScript.CreateObject('Shell.Application').Windows(),
      objFso = WScript.CreateObject('Scripting.FileSystemObject');
  for (var objEnum = new Enumerator(colShWindows); !objEnum.atEnd(); objEnum.moveNext())
    if (objFso.GetFileName(objEnum.item().FullName).toLowerCase() == 'iexplore.exe')
      WScript.Echo(objEnum.item().LocationURL);
} catch (e) {;}
Steffen

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#6 Post by Hackoo » 27 Jul 2018 10:07

aGerman wrote:
26 Jul 2018 16:02
I'm not a member on SO, Hackoo.
Are you familiar with JScript hybrids?
This is a proof of concept how to enumerate the URLs of the InternetExplorer tabs.

Code: Select all

@if (0)==(0) echo off

for /f "delims=" %%i in ('cscript //nologo //e:jscript %~fs0') do (
  echo %%i
)
pause

goto :eof @end try {
  var colShWindows = WScript.CreateObject('Shell.Application').Windows(),
      objFso = WScript.CreateObject('Scripting.FileSystemObject');
  for (var objEnum = new Enumerator(colShWindows); !objEnum.atEnd(); objEnum.moveNext())
    if (objFso.GetFileName(objEnum.item().FullName).toLowerCase() == 'iexplore.exe')
      WScript.Echo(objEnum.item().LocationURL);
} catch (e) {;}
Steffen
Thank you @aGerman your script give me an idea to translate it in vbscript, because i'm familar with vbs hybrid , but anyway here is the code in vbscript :

Code: Select all

Set objFso = CreateObject("Scripting.FileSystemObject")	  
Set colShWindows = CreateObject("shell.application").Windows
For Each w In colShWindows
	If objFso.GetFileName(LCase(w.FullName)) = "iexplore.exe" Then
		WScript.Echo w.LocationURL
	End If
Next
And in hybrid code batch and vbscript ;)

Code: Select all

@echo off
(
echo Set objFso = CreateObject^("Scripting.FileSystemObject"^)	  
echo Set colShWindows = CreateObject^("shell.application"^).Windows
echo For Each w In colShWindows
echo 	If objFso.GetFileName^(LCase^(w.FullName^)^) = "iexplore.exe" Then
echo 		WScript.Echo w.LocationURL
echo 	End If
echo Next
)>"%Temp%\iexplore_tabs.vbs"

for /f %%a in ('cscript //nologo "%Temp%\iexplore_tabs.vbs"') do echo %%a
pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#7 Post by Squashman » 27 Jul 2018 10:10

Hackoo wrote:
27 Jul 2018 10:07

And in hybrid code batch and vbscript ;)

Code: Select all

@echo off
(
echo Set objFso = CreateObject^("Scripting.FileSystemObject"^)	  
echo Set colShWindows = CreateObject^("shell.application"^).Windows
echo For Each w In colShWindows
echo 	If objFso.GetFileName^(LCase^(w.FullName^)^) = "iexplore.exe" Then
echo 		WScript.Echo w.LocationURL
echo 	End If
echo Next
)>"%Temp%\iexplore_tabs.vbs"

for /f %%a in ('cscript //nologo "%Temp%\iexplore_tabs.vbs"') do echo %%a
pause
Hybrid means all the code is self contained in one script file an executes the same script file. You are just writing out a vbscript and then executing it.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#8 Post by aGerman » 27 Jul 2018 10:21

The user on SO might be happy with any kind of "piece of code". You can easily do everything in VBS. Just use a WMI query to terminate all instances of iexplore.exe if the URL was found.

Steffen

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#9 Post by Hackoo » 27 Jul 2018 12:19

aGerman wrote:
27 Jul 2018 10:21
The user on SO might be happy with any kind of "piece of code". You can easily do everything in VBS. Just use a WMI query to terminate all instances of iexplore.exe if the URL was found.
Steffen
I did like this !

Code: Select all

@echo off
Title Checking Running Process and Get their command lines
setlocal enabledelayedexpansion
Mode 90,25 & color 0A
Set "ProcessName=iexplore.exe"
Set "String2Search=facebook"
Call :CheckRunning !ProcessName!
If /I "%flag%"=="True" (
    color 0A
    echo %flag%
	Call :GetCommandLine !ProcessName!
	echo(
	Call :CheckString_in_URL "%String2Search%"
) else (
    color 0C
    echo %flag%
    echo !ProcessName! is not running !
    pause>nul & exit
)
Exit
::********************************************************************************************************
:CheckRunning <ProcessName>
Set "ProcessName=%1"
tasklist /NH /FI "imagename eq %ProcessName%" 2>nul |find /i "%ProcessName%" >nul
If not errorlevel 1 ( Set "flag=True" ) else ( Set "flag=False" )
Exit /b 
::********************************************************************************************************
:GetCommandLine <ProcessName>
Set "ProcessCmd="
for /f "tokens=2 delims==" %%P in ('wmic process where caption^="%~1" get commandline /format:value ^| findstr /I "%~1" ^| find /I /V "%~nx0" 2^>nul') do (
    Set "ProcessCmd=%%P"
    echo !ProcessCmd!
)
Exit /b
::******************************************************************************************************
:Kill <ProcessName>
Taskkill /IM "%~1" /F>nul 2>&1
Exit /b
::******************************************************************************************************
:CheckString_in_URL <String2Search>
set "String2Search=%~1"
(
echo Set objFso = CreateObject^("Scripting.FileSystemObject"^)	  
echo Set colShWindows = CreateObject^("shell.application"^).Windows
echo For Each w In colShWindows
echo 	If objFso.GetFileName^(LCase^(w.FullName^)^) = "iexplore.exe" Then
echo 		WScript.Echo w.LocationURL
echo 	End If
echo Next
)>"%Temp%\iexplore_tabs.vbs"

setlocal EnableDelayedExpansion
for /f %%a in ('cscript //nologo "%Temp%\iexplore_tabs.vbs"') do (
	set /a index+=1
	set "URL[!index!]=%%a"
)

for /L %%i in (1,1,%index%) do (
	echo !URL[%%i]! | find /I "%String2Search%">nul && ( 
		echo Found this string "%String2Search%" in the URL "!URL[%%i]!"
		echo(
		echo Did you want to kill this process "!ProcessName!" Y/N ?
		Set /p "Answer="
			If /I "!Answer!"=="Y" ( Call :Kill "!ProcessName!" ) else ( exit )
		) || (
			echo No string like "%String2Search%" found in "!URL[%%i]!"
			Timeout /T 5 /nobreak>nul
		)
)
exit /b 
::******************************************************************************************************

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to check (with "tasklist" built-in cmd) if prgm is running?

#10 Post by aGerman » 27 Jul 2018 12:41

Don't you think the script should rather run unattended?
Uhujkill wrote:I am needing to force close IE when a user locks their PC
Sounds to me like creating a scheduled task that listens to the lock event.

Steffen

Post Reply