WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Console Output to Screen and File

 Keywords: Console Output Screen Text File StdOut AllocConsole SetConsoleScreenBufferSize ReadConsoleOutputCharacterA

; Purpose:
; Run commandline application which writes text output onto the console screen.
; Read text output from console screen into string variable.
; Write string variable into textfile.

GoSub Define_Functions


; Get handle to the console.
hdlKernel32 = DllLoad ("Kernel32")
intResult = DllCall (hdlKernel32, long:"AllocConsole")
hdlStdOut = DllCall (hdlKernel32, long:"GetStdHandle", long:-11)


; To avoid 'folded' lines.
; Specify the new size of the console screen buffer, in character rows and columns.
; Specified dimensions cannot be less than the minimum size allowed by the system.
intBuffCols = 130 ; Adjust this value to your needs.
intBuffRows = 100 ; Adjust this value to your needs.
intBuffCoord = intBuffCols + (intBuffRows << 16)
IntResult = DllCall (hdlKernel32, long:"SetConsoleScreenBufferSize", long:hdlStdOut, long:intBuffCoord)


; Run commandline application.
blnResult = RunWait (Environment ("COMSPEC"), "/C Ipconfig /all")
;blnResult = RunWait (Environment ("COMSPEC"), "/C DIR /-P /A /OGNE C:\")
;blnResult = RunWait (Environment ("COMSPEC"), "/C CHKDSK C:")
;blnResult = RunWait (Environment ("COMSPEC"), "/C Net use")


; Read text stream from screenbuffer.
intFirstCell = 0
intBBScreenLen = intBuffCols * intBuffRows
hdlBBScreen = BinaryAlloc (intBBScreenLen)
BinaryEodSet (hdlBBScreen, intBBScreenLen)

hdlBBRet = BinaryAlloc (4)
BinaryEodSet (hdlBBRet, 4)

intResult = DllCall (hdlKernel32, long:"ReadConsoleOutputCharacterA", long:hdlStdOut, lpbinary:hdlBBScreen, long:intBBScreenLen, long:intFirstCell, lpbinary:hdlBBRet)
intResult = DllCall (hdlKernel32, long:"CloseHandle", long:hdlStdOut)
intResult = DllCall (hdlKernel32, long:"FreeConsole")

intConOutLen = BinaryPeek4 (hdlBBRet, 0) ; Length of console output character stream.
hdlBBRet = BinaryFree (hdlBBRet)

intEod = BinaryConvert (hdlBBScreen, 1, 0, 0, 0) ; Convert OEM to ANSI.

strConOut = BinaryPeekStr (hdlBBScreen, 0, intBBScreenLen)
hdlBBScreen = BinaryFree (hdlBBScreen)


; Adjust text stream by linebreaks.
strConOut = StrSub (StrTrim (Num2Char (1) : strConOut), 2, -1) ; Remove trailing spaces.
intStrLen = StrLen (strConOut)
intPos = -1
While intPos < intStrLen
   intPos = intPos + intBuffCols + 2
   strConOut = StrInsert (strConOut, @CRLF, "", intPos)
EndWhile


; Put text into file.
strFilename = udfFileCreateTemp ("Con", "txt")
intResult = FilePut (strFilename, strConOut)

blnResult = RunWait ("notepad.exe", strFilename)
blnResult = FileDelete (strFilename)

Exit

;==========================================================================================================================================
:Define_Functions
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileCreateTemp (strPrefix, strExtension)
strFilenameTemp = FileCreateTemp (strPrefix)
strFilename = strFilenameTemp
If strExtension != "" Then strFilename = ItemReplace (strExtension, -1, strFilenameTemp, ".")
blnResult = FileRename (strFilenameTemp, strFilename)
Return strFilename
;..........................................................................................................................................
; This UDF "udfFileCreateTemp" creates a 0-byte file with unique name in the user's temporary folder
; (as specified by the "TMP" or "TEMP" environment variable).
;
; The prefix string can be set by parameter strPrefix and will be truncated to 3 chars.
; The file extension string can be set by parameter strExtension.
; If strExtension is empty, then the file extension is set to "tmp".
;
; The WinBatch FileCreateTemp function can create maximal 65535 temporary files
; of the form "hexnumber.tmp" from "1.tmp" to "FFFF.tmp".
; One more attempt will create WB error 1653.
;
; Detlev Dalitz.20090521.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
Return ; from Gosub Define_Functions
;==========================================================================================================================================
; Detlev Dalitz.20090606.

Article ID:   W18240
Filename:   Console Output to Screen and File .txt
File Created: 2009:06:08:09:19:02
Last Updated: 2009:06:08:09:19:02