FileAppend to stdout/stderr

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

FileAppend to stdout/stderr

20 Feb 2018, 23:25

- I have some concerns re. the following lines:

Code: Select all

;FileAppend, % vOutput, *, UTF-8
FileAppend, % vOutput, **, UTF-8
;FileAppend, % vOutput, * *, UTF-8 ;doesn't work
;FileAppend, % vOutput, * **, UTF-8 ;doesn't work

;FileAppend, % vOutput, %vPath%, UTF-8
;FileAppend, % vOutput, * %vPath%, UTF-8
- There are two lines that don't work, however, they appear to be legitimate code.
- Also, if they don't work, then the lines using the vPath variable are less versatile, and cannot accept * or **.

- There is a further problem, either ** always appends to StdOut (it should append to StrErr), or, the retrieve StdErr function below doesn't work (and it always retrieves the StdOut).
- Script 1: Retrieve text.

Code: Select all

;FileAppend
;https://autohotkey.com/docs/commands/FileAppend.htm

q:: ;test append stdout+stderr
vPath := A_Desktop "\test append stdout+stderr.ahk"
vTarget = "%A_AhkPath%" "%vPath%"
MsgBox, % JEE_RunGetStdOut(vTarget)
MsgBox, % JEE_RunGetStdErr(vTarget)
;MsgBox, % StrLen(JEE_RunGetStdOut(vTarget))
;MsgBox, % StrLen(JEE_RunGetStdErr(vTarget))
return

;==================================================

;JEE_RunWaitGetStdOut
JEE_RunGetStdOut(vTarget, vSize:="")
{
	DetectHiddenWindows, On
	vComSpec := A_ComSpec ? A_ComSpec : ComSpec
	Run, % vComSpec,, Hide, vPID
	WinWait, % "ahk_pid " vPID
	DllCall("kernel32\AttachConsole", UInt,vPID)
	oShell := ComObjCreate("WScript.Shell")
	oExec := oShell.Exec(vTarget)
	vStdOut := ""
	if !(vSize = "")
		VarSetCapacity(vStdOut, vSize)
	while !oExec.StdOut.AtEndOfStream
		vStdOut := oExec.StdOut.ReadAll()
	DllCall("kernel32\FreeConsole")
	Process, Close, % vPID
	return vStdOut
}

;==================================================

;JEE_RunWaitGetStdErr
JEE_RunGetStdErr(vTarget, vSize:="")
{
	DetectHiddenWindows, On
	vComSpec := A_ComSpec ? A_ComSpec : ComSpec
	Run, % vComSpec,, Hide, vPID
	WinWait, % "ahk_pid " vPID
	DllCall("kernel32\AttachConsole", UInt,vPID)
	oShell := ComObjCreate("WScript.Shell")
	oExec := oShell.Exec(vTarget)
	vStdErr := ""
	if !(vSize = "")
		VarSetCapacity(vStdErr, vSize)
	while !oExec.StdErr.AtEndOfStream
		vStdErr := oExec.StdErr.ReadAll()
	DllCall("kernel32\FreeConsole")
	Process, Close, % vPID
	return vStdErr
}

;==================================================
- Script 2: Append text.

Code: Select all

vOutput := "a`r`nb`r`nc`r`nd`r`ne" ;length 13
;vOutput := "a`r`n" Chr(8730) "`r`nc"
;vOutput := "a`nb`nc`nd`ne" ;length 9
;FileAppend, % vOutput, *, UTF-8
FileAppend, % vOutput, **, UTF-8
;FileAppend, % vOutput, * *, UTF-8
;FileAppend, % vOutput, * **, UTF-8
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileAppend to stdout/stderr

16 Mar 2018, 08:58

Bump.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: FileAppend to stdout/stderr

17 Mar 2018, 03:06

Hello :wave:.
There are two lines that don't work, however, they appear to be legitimate code.

Code: Select all

;FileAppend, % vOutput, * *, UTF-8 ;doesn't work
;FileAppend, % vOutput, * **, UTF-8 ;doesn't work
Assuming you mean these lines, what do you expect from them? Quick reading, I do not see anything in the documentation suggesting it would mean something. It throws an exception, and sets a_lasterror to 123
ERROR_INVALID_NAME
123 (0x7B)
The filename, directory name, or volume label syntax is incorrect.
src.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileAppend to stdout/stderr

17 Mar 2018, 04:45

Hello :wave:. Three places where asterisks can be used. Whenever I append anything I want to disable EOL translation.
To disable EOL translation, prepend an asterisk to the filename.
Standard Output (stdout): Specifying an asterisk (*) for Filename causes Text to be sent to standard output (stdout).
Specifying two asterisks (**) for Filename causes Text to be sent to the stderr stream.
My main concern now though, is re. whether stdout or stderr is being appended to/read from.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: FileAppend to stdout/stderr

17 Mar 2018, 04:57

[b]GetStdHandle[/b] wrote:STD_OUTPUT_HANDLE (DWORD)-11
The standard output device. Initially, this is the active console screen buffer, CONOUT$.
STD_ERROR_HANDLE (DWORD)-12
The standard error device. Initially, this is the active console screen buffer, CONOUT$.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: FileAppend to stdout/stderr

17 Mar 2018, 12:44

As far as I can tell from the documentation, the disable-EOL-translation * is for when the parameter Filename is a filename, only. Do you need AHK to handle EOL differently than it does when using stdout * and stderr **?

I tried the first example for stdout in the documenation, it threw an exception as well :think:. Probably helgef error :yawn:.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileAppend to stdout/stderr

17 Mar 2018, 12:58

- It's not *necessary* as such, it's more for performance.
specifying the asterisk when Text contains `r`n improves performance because the program does not need to scan Text for `r`n
- It was a curio that emerged: (a) when writing a converter script, (b) I always use *, but then I realised I wasn't using it when appending to stdout/stderr. The AHK v2 syntax is slightly different, and makes it possible to do both at the same time.
- Re. the other problem, stdout v. stderr, I suppose just me is suggesting I can try the appending via DllCall instead. Hmm. It's a pretty major issue, either the appending, or the reading, is not working correctly.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileAppend to stdout/stderr

17 Mar 2018, 13:19

OK, this is working! My functions that read from stdout and stderr work correctly.

Code: Select all

;FileOpen
;https://autohotkey.com/docs/commands/FileOpen.htm#commands/FileOpen.htm

DllCall("kernel32\AllocConsole")
oFile := FileOpen("*", "w") ;for stdout
oFile.Write("STDOUT: abc`r`ndef`r`nghi")
oFile.Close()

DllCall("kernel32\AllocConsole")
oFile := FileOpen("**", "w") ;for stderr
oFile.Write("STDERR: abc`r`ndef`r`nghi")
oFile.Close()

MsgBox
Last edited by jeeswg on 31 Jul 2019, 07:48, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileAppend to stdout/stderr

17 Mar 2018, 13:23

Maybe there's a bug, both of these appear to append to stdout, and neither to stderr:

Code: Select all

vOutput := "a`r`nb`r`nc`r`nd`r`ne"
FileAppend, % "STDOUT: " vOutput, *
FileAppend, % "STDERR: " vOutput, **
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: FileAppend to stdout/stderr

24 Mar 2018, 02:05

Yes, that's a bug (FileAppend fails to detect **).

The asterisk prefix is just that, not "asterisk and optional space". If you include a space, it becomes part of the path/filename. Stdout with asterisk prefix, if it was allowed, would be just **, which the documentation clearly states is stderr. "Specifying an asterisk (*) for Filename" means exactly what it says, and precludes the possibility of an optional prefix.

FileAppend does not perform newline translation when writing to stdout.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, mikeyww, Rohwedder and 135 guests