udfCaptureDosOutput
arr udfCaptureDosOutput (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfCaptureDosOutput (strDosCommand)
DebugTrace (22, "")         ; Allow DebugTrace continuation (inherit the debug mode from the caller).
arrData = ArrDimension (4)  ; Allocate return array.
ArrInitialize (arrData, "") ; Initialize to all null strings.
; Stuff the command in the 4th element of the array.
arrData[3] = strDosCommand
objShell = ObjectCreate ("WScript.Shell") ; Open shell object.

; If 64 bit turn off redirection?
If WinMetrics (-7) == 2
   ; 64-bit Windows.
   intIC92old = IntControl (92, "disable", 0, 0, 0)
   objScriptExec = objShell.Exec(strDosCommand) ; Run the command.
   IntControl (92, "revert", intIC92old, 0, 0)
Else
   objScriptExec = objShell.Exec(strDosCommand) ; Run the command.
EndIf

; Capture data from StdOut and StdErr.
objStdOut = objScriptExec.StdOut
objStdErr = objScriptExec.StdErr
While @TRUE
   While !objStdOut.AtEndOfStream
      strLine = objStdOut.ReadLine
      arrData[1] = arrData[1] : strLine : @CRLF
   EndWhile
   While !objStdErr.AtEndOfStream
      strLine = objStdErr.ReadLine
      arrData[2] = arrData[2] : strLine : @CRLF
   EndWhile
   If objScriptExec.Status != 0
      ; Get remainder of data, if any.
      While !objStdOut.AtEndOfStream
         strLine = objStdOut.ReadLine
         arrData[1] = arrData[1] : strLine : @CRLF
      EndWhile
      While !objStdErr.AtEndOfStream
         strLine = objStdErr.ReadLine
         arrData[2] = arrData[2] : strLine : @CRLF
      EndWhile
      Break
   EndIf
EndWhile

arrData[0] = objScriptExec.ExitCode ; Save errorlevel/exit code.

; Close handles.
objStdOut = 0
objStdErr = 0
objScriptExec = 0
objShell = 0

; Return the array.
Return arrData
;------------------------------------------------------------------------------------------------------------------------------------------
; Adapted from ...
;   Topic:  ftype equivalent?
;   Conf:  WinBatch
;   From:  deanad
;   Date:  Tuesday, December 21, 2010 08:49 AM
;
; ... and modified by ...
; 20101221.Detlev Dalitz.
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction


#DefineFunction udfDisplayResult (arrData)
intExitcode = arrData[0]
strStdOut = arrData[1]
strStdErr = arrData[2]
strDosCommand = arrData[3]
strMsgTitle = "CaptureDosOutput"
If strStdErr != ""
   strMsgText = "EXITCODE = " : intExitcode : @LF : StrReplace (strStdErr, @CRLF, @LF)
Else
   strMsgText = "EXITCODE = " : intExitcode : @LF : StrReplace (strStdOut, @CRLF, @LF)
EndIf
IntControl (28, 1, 0, 0, 0)
IntControl (63, 100, 150, 900, 850)
AskItemlist (strMsgTitle, strMsgText, @LF, @UNSORTED, @SINGLE)
Return
;------------------------------------------------------------------------------------------------------------------------------------------
; Adapted from ...
;   Topic:  ftype equivalent?
;   Conf:  WinBatch
;   From:  deanad
;   Date:  Tuesday, December 21, 2010 08:49 AM
;
; ... and modified by ...
; 20101221.Detlev Dalitz.
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



; Example for getting the installpath of the Excel application by calling the DOS command FTYPE.
strDosCmd = Environment ("ComSpec") : ' /C FTYPE | FIND.EXE "Excel.Workspace"'
arrData = udfCaptureDosOutput (strDosCmd)
strResult = ItemExtract (2, arrData[1], '"')
Pause ("Result", strResult)


; Example for Exitcode=1 and StdErr output.
strDosCmd = Environment ("ComSpec") : ' /C CD /D Z:\TEMP\"'
arrData = udfCaptureDosOutput (strDosCmd)
Pause ("Result", "Exitcode and StdErr output ...")
udfDisplayResult (arrData)


; Example for a long list.
strDosCmd = Environment ("ComSpec") : ' /C DIR /A /-P /O:GNE "C:\WINDOWS\SYSTEM32\*.*"'
; Note: Cannot capture CMD /U unicode output!
; This would run into a loop: "While !objStdOut.AtEndOfStream" !!!
arrData = udfCaptureDosOutput (strDosCmd)
Pause ("Result", "Long list ...")
udfDisplayResult (arrData)

:CANCEL
Exit