How to check (with "tasklist" built-in cmd) if prgm is running?
Moderator: DosItHelp
How to check (with "tasklist" built-in cmd) if prgm is running?
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
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
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
TASKLIST does neither set the errorlevel nor does it write to StdErr. Usually I pass the output to FINDSTR which changes the errorlevel accordingly.
Steffen
EDIT: If the process name exceeds the column width you might rather change the output format like that
Code: Select all
set "proc=myprog.exe"
tasklist /nh /fi "imagename eq %proc%" | >nul findstr /bic:"%proc% "
echo %errorlevel%
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%\","
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
You can take a look at this : https://stackoverflow.com/questions/514 ... ve#tab-top
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
Code: Select all
FOR /F "skip=1 delims=" %%G IN ('tasklist /FO CSV /FI "imagename eq notepad.exe"') do echo It is Running.
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
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.
Steffen
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) {;}
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
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 :aGerman wrote: ↑26 Jul 2018 16:02I'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.SteffenCode: 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) {;}
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
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
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
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.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
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
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
Steffen
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
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
::******************************************************************************************************
Re: How to check (with "tasklist" built-in cmd) if prgm is running?
Don't you think the script should rather run unattended?
Steffen
Sounds to me like creating a scheduled task that listens to the lock event.Uhujkill wrote:I am needing to force close IE when a user locks their PC
Steffen