Page 2 of 2
Re: Check for running exe program
Posted: 26 Apr 2015 16:46
by klownboy
Thanks highend, but I don't need a change. I have no problem specifying the whole exe filename including extension especially knowing that's how the file is displayed in Task Manager. I was only testing to see if it needed the extension and what the results would be. Thanks.
Re: Check for running exe program
Posted: 26 Apr 2015 18:06
by highend
I modified it a bit more...
It supports the "?=any single character" & "*=zero or more characters" wildcard chars now.
You can now either use the process name with / without the trailing .exe and any combination of the wildcard chars.
This saves you from using a foreach loop if you want to test for running processes that share a common pattern.
E.g.
Code: Select all
if (isProcessRunning("evernote*")) { echo "is running..."; }
Will return true if any of the following processes are running:
Evernote [Portable].exe
Evernote.exe
EvernoteClipper.exe
EvernoteTray.exe
Code: Select all
function isProcessRunning($process) {
$process = regexreplace($process, "([\[\]^$.+(){}])", "\$1");
$process = """" . replace(replace($process, "?", ".{1}"), "*", ".*?") . "(\.exe|"")";
if (regexmatches(runret("cmd /c @TASKLIST /NH /FO CSV"), $process)) { return 1; }
return;
}
// Example of wildcard use. Will find "FlashPlayerPlugin_17_0_0_134.exe
if (isProcessRunning("?lash*134")) { echo "is running..."; }
Re: Check for running exe program
Posted: 26 Apr 2015 18:20
by klownboy
Cool highend. I tried quite a few different combinations like '?gent and stepping through it I saw 'agent'. I tried *agent and saw 'PDagent' as well as 'agent'. I closed 'agent.exe' and then saw only 'PDagent'. Very nice!
Re: Check for running exe program
Posted: 26 Apr 2015 18:21
by bdeshi
highend,
any reason to not do an exact match? [ed. regarding a deprecated version.]
And also what's the @ doing there?
btw, here's my "fork":
Code: Select all
function isProcessRunning($process) {
//if (regexmatches(runret("cmd /c @TASKLIST /NH"), "^" . regexreplace($process, "([\[\]^$.+(){}])", "\$1"))) { return 1; }
$cmdExe = (%osbitness% == 64) ? "%windir%\sysWOW64\cmd.exe" : "%windir%\system32\cmd.exe" ;
$report = runret(quote($cmdExe) . '/C TASKLIST /NH /FO CSV /FI "IMAGENAME eq '. replace($process, "'", "\'") .'"');
$result = trim(gettoken($report, 1, '","'),'"');
if (recase($result) == recase($process)) { return 1; } else { return 'NOT FOUND'; }
}
text isprocessrunning("xyplorer.exe");
Re: Check for running exe program
Posted: 26 Apr 2015 18:41
by klownboy
Hey Sammay, your version seems to work fine for exact file names, but it may be a bit too exact in that it appears to be case sensitive. XYplorer works; whereas xyplorer.exe does not.
Re: Check for running exe program
Posted: 26 Apr 2015 18:55
by bdeshi
fixed, thanks.
Re: Check for running exe program
Posted: 26 Apr 2015 19:10
by Stef123
Couldn't resist trying it out, too. Works nice as well. What's the difference to highend's version? I noticed some OS bitness business in there?
Re: Check for running exe program
Posted: 26 Apr 2015 19:17
by bdeshi
I heartily recommend highend's latest version ( ^up there^ ).
Mine:
1) always does exact matches
2) and most of the detection is done by system, whereas highend uses some high-end regexp (and I understand he *really* likes regexps...
)
Re: Check for running exe program
Posted: 26 Apr 2015 19:20
by Stef123
SammaySarkar wrote:highend's latest version ( ^up there^ ).
Ooops - how could I miss that update. Thanks for the up-pointer.
Re: Check for running exe program
Posted: 26 Apr 2015 19:59
by klownboy
Thanks Sammay, that did the trick. I suppose you could have used "LikeI" instead of "==" and recasing. It works as well.
Code: Select all
if ($result LikeI $process) { return Found; } else { return 'NOT FOUND'; }
Re: Check for running exe program
Posted: 26 Apr 2015 20:07
by bdeshi
I decided against it, what if the process name had [] etc and they got evaluated? no need to take a risk.
Re: Check for running exe program
Posted: 27 Apr 2015 16:13
by bdeshi
another version of my fork:
Code: Select all
FUNCTION isprocessrunning($process, $data = 'count') {
/*
Checks if a process is running. Checks by process name only, no path.
$process the process to check for. Only filename without path has to be used.
$data what data to return. One of the following
count number of matching processes (default)
pid PIDs of matching processes comma-separated
anything else returns raw results when any process matches
*/
$return = '';
$cmdExe = (%osbitness% == 64) ? "%windir%\sysWOW64\cmd.exe" : "%windir%\system32\cmd.exe" ;
$report = trim(runret(quote($cmdExe) . '/C TASKLIST /FO CSV /FI "IMAGENAME eq '. replace($process, "'", "\'") .'"'), <crlf>);
$result = trim(gettoken(gettoken($report,2,<crlf>), 1, '","'), '"', 'L');
if (recase($result) == recase($process)) {
if ($data == 'pid') {
foreach($line, $report, <crlf>){
$pid = trim(gettoken($line, 2, '","', '"'));
if ($pid != '') && (regexreplace($pid,'^\d*') != '') { continue; } //to skip beginning "pid" field name
$pids = (isset($pids) ? $pids : '') . ',' . $pid;
}
$return = replace($pids, ',', '',,, 1);
}
elseif ($data == 'count') {
//tasklist lists processes one each line
$return = (gettoken($report, 'count', <crlf>) - 1); //discount fieldnames line
}
else { $return = $report; }
}
elseif ($data == 'count') { $return = 0; }
return $return;
}
[edited]