Check for running exe program

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Stef123

Check for running exe program

Post by Stef123 »

Been considering GET and EXISTS and EVAL but couldn't pull it off.
Is there any way to check whether or not a certain .exe is up and running? To complicate matters, it's not visible in the taskbar - which is why I need this check in the first place, before I proceed with my intended script. Instead, the program is hidden in the background, sitting in the systray if I am lucky.

BTW, this seems like material for a reusable function. Wouldn't mind to try this on top of it. Feeling confident today :biggrin:

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Code: Select all

@FOR /F "tokens=1 delims=," %%P IN ('TASKLIST /NH /V /FO CSV ^| FINDSTR /IL "<the process name.exe>"') DO @ECHO ISRUNNING
Send it through runret() and then parse it for "ISRUNNING". Have fun with the quoting stuff xD Alternatively, writefile it as a batch, execute it and pipe it's output in a temp file. readfile it. Parse it.
One of my scripts helped you out? Please donate via Paypal

Stef123

Re: Check for running exe program

Post by Stef123 »

Whoa :shock: :shock: - serves me right, had it coming this way by boasting my hi-flying confidence. :oops: Well, back to ground lessons. Looked up runret - seems doable.

Quotes - always a challenge, reading through my notes I realize I probably should use double quotes instead of single, to get variables resolved - just in case there are any of those things present in your code.

Pipe and parse, however ... :veryconfused: SC help finds it under heredoc syntax, load and getpathcomponent. And what seems more applicable, also under run:
SC help wrote:....quotes have to be wrapped in single-quotes because an argument's outer quotes are auto-stripped by XY's script parser!
Hm - back to square one, single or double quotes?
Wikipedia about parsing ".... the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other". Yeah, right.

So far, my research has taken me nowhere. Complete standstill, not even crawling.
Up til now, I've been "scripting" CTB menus only, with an occasional If-clause in it.
Do you see any chance for me to catch up on this thing? If so, please point me towards SC help topics.

Thanks
Stef

binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: Check for running exe program

Post by binocular222 »

Lazy way: use Autohotkey

Code: Select all

 Process, exist, XYplorer.exe
 Msgbox %Errorlevel%  ;return the process's PID
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Requires an external .exe. Not really an appropriate way to put it into a user function afterwards...
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Just using it as a batchfile is simple. Using the PREFERRED way with runret() is more complicated (because of the necessary quoting / escaping).

Code: Select all

    $batFile    = "%TEMP%\~checkRunning.bat";
    $result     = "%TEMP%\~checkRunningResult.txt";
    $batContent = <<<>>>
@FOR /F "tokens=1 delims=," %%P IN ('TASKLIST /NH /V /FO CSV ^| FINDSTR /IL "xyplorer.exe"') DO @ECHO ISRUNNING>"$result"
>>>;
    writefile($batFile, $batContent);
    run $batFile, , 2, 0;
    if (regexmatches(readfile($result), "isrunning")) {
        msg "Process is running";
     }
     delete 0, 0, "$batFile|$result";
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

As a function:

Code: Select all

function isProcessRunning($process) {
    if (regexmatches(runret("cmd /c @TASKLIST /NH"), regexreplace($process, "^" . "([\[\]^$.+(){}])", "\$1"))) { return 1; }
    return;
}
Call it with:

Code: Select all

if (isProcessRunning("xyplorer.exe")) { echo "is running..."; }
One of my scripts helped you out? Please donate via Paypal

Stef123

Re: Check for running exe program

Post by Stef123 »

Awesome. :tup: :beer:
Getting showered like this with great suggestions - cannot keep up with you guys. Still busy testing and reading and modifying ...

@highend
Your last approach - the function - is my absolute favorite. :appl: Very versatile and easy to handle.
Had some qualms about the one before that - unsure if I'd be granted access to write to TEMP, but after some testing it turns out, no problem. Cannot tell you how glad I am to ditch the very first attempt.
MANY THANKS.

@binocular222
I love AHK. In the past I had even managed to create a user dialog - that doesn't really do much, but looks VERY professional, just like the real stuff you get to see in Windows. ATM still busy reading ahk-help about EXIST and related concepts.
highend wrote:Requires an external .exe. Not really an appropriate way to put it into a user function afterwards...
Does this still apply for .ahk instead of.exe? I run portable AHK from stick, so it travels with me wherever I go.
I thought of turning it around, calling AHK to do those checks, then calling XY and passing it my .xys after ahk has completed its part. Well, that was b4 you posted the super-smart function, but still, for general purposes I wonder if this ahk_2_xys approach holds water?

klownboy
Posts: 4397
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Check for running exe program

Post by klownboy »

Hi highend, I hope all is well. This will be a good one for the function compilation. At the moment though I can't get it to work . It's echoing / returning "process is running" on anything I input as the process (e.g., 'nonexisting.exe' or I close a real running program and call it and it still returns process is running). I included the function in my inc.xyi file and called it properly and I also ran the call with the function contained in the same script. I also ran the batch file and see the flash from cmd but no XY message displays. I take it you're not having problems with a non-existent or not running exe?

Edit: In stepping through it, I'm seeing a return of "1" regardless of what I enter for an exe file. If it helps, when the 'regexmatches' step comes up I see "agent\.exe" - a backslash before the dot when using agent as the exe file to check.
Last edited by klownboy on 26 Apr 2015 14:38, edited 2 times in total.

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Your last approach - the function - is my absolute favorite. :appl: Very versatile and easy to handle.
That's why I created it.
unsure if I'd be granted access to write to TEMP,
%TEMP% points to the temp folder of the current user. It's always writable for him / her (unless somebody messed with it's folder permissions).
Does this still apply for .ahk instead of.exe?
Meaning of having either the compiled .exe or the ahk interpreter at hand? Sure. In all cases it's an external dependency. The tasklist command is available since xp and requires nothing else (it's inbuilt). The only better way would be if Don provides a new scripting command (that uses the windows api) -> faster execution time.
One of my scripts helped you out? Please donate via Paypal

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

klownboy wrote:At the moment though I can't get it to work . It's echoing / returning "process is running" on anything I input as the process (e.g., 'nonexisting.exe' or I close a real running program and call it and it still returns process is running). I included the function in my inc.xyi file and called it properly and I also ran the call with the function contained in the same script
Hi Ken,

Try this. Look at the $output variable when it reaches the step; command. Or contact me via e-mail and I take a look via teamviewer...

Code: Select all

function isProcessRunning($process) {
    $output = runret("cmd /c @TASKLIST /NH");
    step; // DEBUG
    if (regexmatches($output, regexreplace($process, "([\[\]^$.+(){}])", "\$1"))) { return 1; }
    return;
}


if (isProcessRunning("xyplorer.exe")) { echo "is running..."; }
I take it you're not having problems with a non-existent or not running exe?
No, for me it works fine. If I use "xyplorer.exe" it finds it, if I use "xyplorer.exes" it's not.
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4397
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Check for running exe program

Post by klownboy »

I think I know why it was choking on my test exe file. I was using "agent.exe", a newsgroup program. Well it turns out I aslo have a background process called PDagent.exe from Perfect Disk, a defrag program. So, when I ran the check on agent.exe it looks like it picked up and used PDagent.exe. After killing PDagent process, it seems to work. Though I guess the question is, should the RegEx Match/Replace have considered "PDagent.exe" as a match for "agent.exe"?

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Though I guess the question is, should the RegEx Match/Replace have considered "PDagent.exe" as a match for "agent.exe"?
No, but you said that you've used e.g. "nonexisting.exe" and it still found it running. What about that case?

A fix for your problem:

Code: Select all

function isProcessRunning($process) {
    if (regexmatches(runret("cmd /c @TASKLIST /NH"), "^" . regexreplace($process, "([\[\]^$.+(){}])", "\$1"))) { return 1; }
    return;
}
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4397
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Check for running exe program

Post by klownboy »

Your new function worked fine for me. I even did a restart such that PDagent.exe was back running since I had killed the process. It properly made the distinction between 'agent' and 'PDagent'. I also tried Blueagent and other bogus file names and it worked fine. I never actually used nonexistent.exe but was probably trying some modification to "agent'. I can't remember exactly now, but in any case it's working fine now. By the way I tested "agent" without specifying the extension and it worked properly and specified 'agent2' and it also worked (i.e., no return). Thanks again.

highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Check for running exe program

Post by highend »

Be careful when you do not add the extension.

Process names:
agent.exe
agentofanotherprocess.exe

When you only specify "agent" and the "agent.exe" is not running but the "agentofanotherprocess.exe" is, you'll get a running message...
One of my scripts helped you out? Please donate via Paypal

Post Reply