Check for running exe program

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Check for running exe program

Post 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.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13260
Joined: 06 Feb 2011 00:33

Re: Check for running exe program

Post 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..."; }
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Check for running exe program

Post 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! :appl:
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Check for running exe program

Post by bdeshi »

highend, any reason to not do an exact match? [ed. regarding a deprecated version.]

And also what's the @ doing there? :whistle:

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");
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Check for running exe program

Post 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.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Check for running exe program

Post by bdeshi »

fixed, thanks. :oops:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Stef123

Re: Check for running exe program

Post 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?

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Check for running exe program

Post 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... :wink: )
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Stef123

Re: Check for running exe program

Post by Stef123 »

SammaySarkar wrote:highend's latest version ( ^up there^ ).
Ooops - how could I miss that update. Thanks for the up-pointer.

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: Check for running exe program

Post 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'; }
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Check for running exe program

Post by bdeshi »

I decided against it, what if the process name had [] etc and they got evaluated? no need to take a risk. :wink:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Check for running exe program

Post 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]
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Post Reply