Convert run and &PID from v1

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Convert run and &PID from v1

20 May 2024, 14:23

Code: Select all

RunWait %comspec% /c " "%ExeFile%" %RunArgs% "%ToZip%" "%SourceFile%" | "%Mtee%" /+ "%Output%" ",, UseErrorLevel, ZipPID
RunWait, %Output%
This work in V1

But this doesn't in v2

Code: Select all

runArgs := " /c ftp " ipAdr " | " Mtee " /+ " outFile ",," &tstPID
Run A_ComSpec runArgs
or

Code: Select all

Run (A_ComSpec " /c ftp " ipAdr " | " Mtee " /+ " outFile ",, " &tstPID )
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Convert run and &PID from v1

20 May 2024, 14:55

Hello,

"Doesn't work" says nothing about what your script does, and nothing about what your script should do.

I did not test it, but a few tips are below.
  1. One script has two RunWait commands. The other does not use RunWait and does not have two such calls, but only one, so there are at least two differences here.
  2. If you replace Run with MsgBox, you might be able to spot a problem with the command line.
  3. If the command line looks OK with your MsgBox, you can test it in a Windows batch file to see if the same command line works there.
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Convert run and &PID from v1

20 May 2024, 15:58

It is not easy to test by someone else, because information is missing.
The example from v1 above, works.

In this example below I got the tstPID from the instruction

Code: Select all

RunWait A_ComSpec " /k dir C:\",,, &tstPID
MsgBox "- " tstPID
= - . _ . - = - . _ . - = - . _ . - = - . _ . - = - . _ . - =

but this doesn't work.

Code: Select all

runArgs := " /c dir C:\"
RunWait A_ComSpec " runArgs "
= - . _ . - = - . _ . - = - . _ . - = - . _ . - = - . _ . - =

In this example below is the file "output" correct created. (but no tstPID)

Code: Select all

runArgs := " /c ftp " ipAdr " | " Mtee " /+ " outFile
Run A_ComSpec runArgs
= - . _ . - = - . _ . - = - . _ . - = - . _ . - = - . _ . - =

But I want to get the PID from the cmd-window.
and have tested a number of different variants - but nothing works. (even with MsgBox)

One problem is where to place the variable for PID &tstPID (before or after the pipe |)
Then configure the string
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Convert run and &PID from v1

20 May 2024, 17:06

The pipe is not an AHK parameter; it is part of your command line. The PID is an AHK parameter. Thus, use the syntax that AHK provides, as follows.

ExitCode := RunWait(Target [, WorkingDir, Options, &OutputVarPID])

The target is your entire command line.

Some programs will not return a PID or will return an unexpected PID, such as if the program runs more than one process. I recommend using HWND as a first choice in most situations. You can get a window's HWND after the window exists.

Having a PID is not very handy with RunWait, because the script will not proceed until the program that you run is closed. At that point, the PID is useless. This means that you might not have thought through exactly what your script is doing, step by step.

Code: Select all

#Requires AutoHotkey v2.0
target     := A_ComSpec ' /k dir ' A_WinDir
workingDir := ''
options    := ''
ExitCode   := Run(target, workingDir, options, &pid)
Sleep 900
MsgBox pid '`n' WinGetPID('ahk_exe WindowsTerminal.exe'), 'PID', 'Iconi'

Code: Select all

#Requires AutoHotkey v2.0
target   := A_ComSpec ' /k dir ' A_WinDir
exitCode := Run(target)
hWnd     := WinWait('ahk_exe WindowsTerminal.exe')
MsgBox hWnd, 'HWND', 'Iconi'
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Convert run and &PID from v1

24 May 2024, 07:06

thank you!
I like PID better, because PID easier to be checked.
I understand that HWND is better in some cases, (and I dont know how to get the HWND in this example)
but in this case I don't think it matters.

finally I got it working
- however I couldn't get the result from ftp to a variable directly but had to read in the created file

Code: Select all

runArgs := " /c ftp " ipAdr " | " Mtee " /+ " outFile
Run A_ComSpec runArgs,,, &tstPID
MsgBox "Done! `n- " tstPID
What is WindowsTerminal.exe? (Is there something I need to install?)
Can that program add anything to me?
I don't understand your two example. What should these do?

Have looked at PowerShell - a powerful tool, but haven't seen anyone use it together with AHK.
(or some example in the AHK-documentation)
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Convert run and &PID from v1

24 May 2024, 07:40

The first example shows how you can retrieve an unexpected PID or the wrong PID. The second example shows how to get a window's HWND after you run a program. My understanding is that Windows 11 (and perhaps Windows 10, but I do not recall) changed the programs used for CMD shell; not a great concern.

If you have not seen anyone use PowerShell with AHK, then you have not searched the forum for any posts about this. You will not find any search results without conducting the search!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Kintaro-OEx, ntepa and 66 guests