Send the output of program execution to a variable Topic is solved

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

Send the output of program execution to a variable

16 May 2024, 03:31

Hi!
This command sends the output to a file. (from example 4 in the ahk-documentation)

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
RunWait A_ComSpec " /c dir C:\ >>C:\temp\runTest.txt", , "Min"
Run "C:\temp\runTest.txt"
Persistent
1) The file runTest.txt is created in ANSI
Is it possible to create the result in UTF-8 instead?
How?

2) In the example above, the result is displayed in a program (notepad)
My wish is that the result directly end up in a variable/an object, so that it can then be analysed.
Is it possible?
How?
reddyshyam
Posts: 58
Joined: 24 Jul 2023, 04:34

Re: Send the output of program execution to a variable  Topic is solved

16 May 2024, 03:41

Hi @Albireo ,

Below will get the result to clipboard.

Code: Select all

	RunWait A_ComSpec " /c dir C:\  | clip ", , "Min"
	MsgBox A_Clipboard
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Send the output of program execution to a variable

16 May 2024, 05:38

Thank you!
But there is something I don't understand.

If I run these instructions (with correct ip address),
I would have hoped that the response from the ftp server would end up in the Clipboard

Code: Select all

Clipboard := ""
ipAdr := '??.??.??.??'
Run A_ComSpec " /c ftp " ipAdr "  | clip "
MsgBox A_Clipboard
But...
On the screen I can see an empty black CMD-window, and the MsgBox (with Clipboard) is show the copy I made before the run of this script.

Even this script does not clear the Clipboard

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
Clipboard := ""
MsgBox A_Clipboard
What am I doing wrong?
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Send the output of program execution to a variable

16 May 2024, 10:15

In my search for a solution, I use a program called Mtee.exe
Which I also attach
mtee.zip
(7.98 KiB) Downloaded 35 times

This script displays the response from the command ftp ??.??.??.?? in the CMD-window,
but also creates a text file with the same content.
A_Clipboard in the MsgBox, displays the last copy before the script was executed.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

A_Clipboard := ""
Mtee := "C:\temp\mtee.exe"
outFile := "c:\Temp\ftpTest.txt"
ipAdr := '??.??.??.??'

runArgs := " /c ftp " ipAdr " | " Mtee " " outFile
Run A_ComSpec runArgs

MsgBox "Done! `n" A_Clipboard
My wish is for the result, to end up in the Clipboard instead of in a file - Is that possible?

I have tried something with clip - but it doesn't work at all.
(i dont understand clip)
TAC109
Posts: 1154
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Send the output of program execution to a variable

16 May 2024, 20:38

Albireo wrote:
16 May 2024, 05:38
Thank you!
But there is something I don't understand.

If I run these instructions (with correct ip address),
I would have hoped that the response from the ftp server would end up in the Clipboard

Code: Select all

Clipboard := ""
ipAdr := '??.??.??.??'
Run A_ComSpec " /c ftp " ipAdr "  | clip "
MsgBox A_Clipboard
But...
On the screen I can see an empty black CMD-window, and the MsgBox (with Clipboard) is show the copy I made before the run of this script.

Even this script does not clear the Clipboard

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
Clipboard := ""
MsgBox A_Clipboard
What am I doing wrong?
Replying to this earlier post:
  • Your references to 'Clipboard' should be to 'A_Clipboard'.
  • 'Run' should be 'RunWait'.
Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
Seven0528
Posts: 497
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Send the output of program execution to a variable

16 May 2024, 21:02

 @Albireo
I believe this method is the cleanest, but I don't fully understand the code.
I'm especially unsure about the WScript.Shell part, as it is outside the scope of AHK. Please take note of this.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
result := getHiddenCommandOutput("dir C:\")
msgbox result

getHiddenCommandOutput(command)    {
    result  := ""
    detectHiddenWindows (prevDHW := A_DetectHiddenWindows, true)
    run(A_ComSpec,, "Hide", &pid)
    if (winWait("ahk_pid " pid,, 1))    {
        if (dllCall("Kernel32.dll\AttachConsole", "UInt",pid))    {
            try result := comObject("WScript.Shell").Exec(A_ComSpec " /c " command).StdOut.ReadAll()
            dllCall("Kernel32.dll\FreeConsole")
        }
        winClose("ahk_pid " pid)
    }
    detectHiddenWindows prevDHW
    return result
}

Code: Select all

 Volume in drive C has no label.
 Volume Serial Number is 0000-0000

 Directory of C:\

00/00/0000  00:00 AM    <DIR>          _
00/00/0000  00:00 AM                 0 _
              00 File(s)              0 bytes
              00 Dir(s)               0 bytes free

I cannot test it in my environment, but please try the following code:

Code: Select all

ipAdr := '??.??.??.??'
result := getHiddenCommandOutput("ftp " ipAdr)
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 497
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Send the output of program execution to a variable

17 May 2024, 14:02

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
ShellHidden.create()
msgbox ShellHidden.ComObj.Exec(A_ComSpec " /c dir C:\").StdOut.ReadAll()

Code: Select all

class ShellHidden ;  ahk2.0
{
    static _pid                 := 0
        ,_isConsoleAttached     := false
        ,_shell                 := ""
        ,_objbmOnApplicationExit:= objBindMethod(this, "_onApplicationExit")
        
    static ComObj => this._shell
    static create(timeout := 1)    {
        if (this._shell)
            return true
        onExit(this._objbmOnApplicationExit)
        try  {
            run(A_ComSpec,, "Hide", &pid)
        }  catch  {
        }  else  {
            prevDHW := detectHiddenWindows(true)
            if (winWait("ahk_pid " pid,, timeout))
                this._pid := pid
            detectHiddenWindows(prevDHW)
        }
        if (this._pid)    {
            if (this._isConsoleAttached := dllCall("Kernel32.dll\AttachConsole", "UInt",this._pid))
                try this._shell := comObject("WScript.Shell")
        }
        return (!!this._shell)
    }
    static destroy()    {
        if (!this._shell)
            return false
        this._shell := ""
        if (this._isConsoleAttached)
            this._isConsoleAttached := !dllCall("Kernel32.dll\FreeConsole")
        if (this._pid)    {
            prevDHW := detectHiddenWindows(true)
            if (winExist("ahk_pid " this._pid))
                winClose("ahk_pid " this._pid)
            detectHiddenWindows(prevDHW)
            this._pid := 0
        }
        onExit(this._objbmOnApplicationExit, 0)
        return true
    }
    static _onApplicationExit(*)    {
        this.destroy()
    }
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Send the output of program execution to a variable

17 May 2024, 17:41

Thanks!
Seven0528 wrote:
16 May 2024, 21:02
....
I cannot test it in my environment, but please try the following code:

Code: Select all

ipAdr := '??.??.??.??'
result := getHiddenCommandOutput("ftp " ipAdr)
I think this is more difficult to solve.
The main reason is that dir produces its own result, but the ftp command does not produce any result at all, it is the receiving ftp server as answer the commands, I can only see the result from the communication.
This is where Mtee comes in, and tries to handle StdOut

I made another reflection.
The short solution by @reddyshyam

Code: Select all

RunWait A_ComSpec " /c dir C:\  | clip ", , "Min"
MsgBox A_Clipboard
Give one result

Directory of C:\tmp

2024-05-17 23:51 <DIR> .
2024-05-17 23:51 <DIR> ..
2023-01-27 12:09 5 119 Backup Lazzlo v2.ahk
2023-10-31 11:43 1 098 dir.txt



And the soulutions by @Seven0528 (both give the same result)

Directory of C:\tmp

2024-05-17 23:51 <DIR> .
2024-05-17 23:51 <DIR> ..
2023-01-27 12:09 5ÿ119 Backup Lazzlo v2.ahk
2023-10-31 11:43 1ÿ098 dir.txt
2023-11-25 11:33 63ÿ769 OCR-A Regular.ttf
3 File(s) 69ÿ986 bytes
2 Dir(s) 263ÿ207ÿ047ÿ168 bytes free

The difference is ÿ between numbers (I don't know why)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: boardtc, DavidP, mikeyww, vanove and 65 guests