Get FilePath of A Running Script

Get help with using AutoHotkey and its commands and hotkeys
scriptor2016
Posts: 491
Joined: 21 Dec 2015, 02:34

Get FilePath of A Running Script

04 Apr 2019, 23:54

Hi again

Basically, I have a script running which is entitled "MyScript.ahk"

I am trying to get the full path of the .ahk file (for example, I am trying to get C:\Windows\MyScripts\MyScript.ahk)

I found a few snippets on the forums and found a way to do it by retrieving the .ahk file's PID, and it is very close - but it is also returning the path of the .exe as well - am am trying to omit that part.

Here is the code:

Code: Select all


DetectHiddenWindows, On   
SetTitleMatchMode, RegEx  

Script := "MyScript.ahk"  

WinGet, PID, PID, % "i)^.+\\" CleanEx(Script) " - AutoHotkey v.+$"
wmi := ComObjGet("winmgmts:")
queryEnum := wmi.ExecQuery(""
. "Select * from Win32_Process where ProcessId=" . pid)
._NewEnum()
    
if queryEnum[process]
MsgBox 0, Command line, % process.CommandLine
else
MsgBox Process not found!
    
wmi := queryEnum := process := ""
return

CleanEx(Needle)
{ ; Sanitize the RegEx input
    StringReplace, Needle, Needle, \, \\, All
    StringReplace, Needle, Needle, ., \., All
    StringReplace, Needle, Needle, *, \*, All
    StringReplace, Needle, Needle, ?, \?, All
    StringReplace, Needle, Needle, +, \+, All
    StringReplace, Needle, Needle, [, \[, All
    StringReplace, Needle, Needle, `{, \`{, All
    StringReplace, Needle, Needle, |, \|, All
    StringReplace, Needle, Needle, (, \(, All
    StringReplace, Needle, Needle, ), \), All
    StringReplace, Needle, Needle, ^, \^, All
    StringReplace, Needle, Needle, $, \$, All
    Return Needle
}
return
User avatar
hoppfrosch
Posts: 347
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Get FilePath of A Running Script

05 Apr 2019, 00:02

scriptor2016
Posts: 491
Joined: 21 Dec 2015, 02:34

Re: Get FilePath of A Running Script

05 Apr 2019, 00:14

Well, I have a listview which displays all my running scripts. Some scripts are in different folders. So when I click on one of the scripts in the listview, I want to be abel to find exactly what folder that script is running from.
Odlanir
Posts: 365
Joined: 20 Oct 2016, 08:20

Re: Get FilePath of A Running Script

05 Apr 2019, 04:28

Why not:

Code: Select all

...
if queryEnum[process] {
    MsgBox 0, Command line, % StrSplit(process.CommandLine, """")[4]
...
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
scriptor2016
Posts: 491
Joined: 21 Dec 2015, 02:34

Re: Get FilePath of A Running Script

05 Apr 2019, 08:42

Hi Odlanir - perfect :) The filepath is now showing without the .exe before it. I knew it had something to do with that single line of code but it's way over my head... many thanks :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Flipeador, hannahelappin, hello9876123, john_c, MannyKSoSo, njs, WilburBr and 161 guests