Jump to content

StdoutRead to Array?


jfitty
 Share

Recommended Posts

Hi all,

Is it possible to run a command using StdoutRead to capture the output, then insert that straight into an array? I know I could send the output of the command to a text file and use _FileReadToArray, but I would rather the output go straight to an array

Thanks in advance

Link to comment
Share on other sites

  • 2 weeks later...

What character do you want to split the array on? new line?

$sText = StdoutRead (...)
$aArray = StringSplit ($sText, @CRLF)

Thats a simple way to do it.

Mat

Only issue is with non-standard characters - any simple fix for this? (I take it that we're talking an ANSI/UNICODE issue?)

#include <Array.au3>
#include <Constants.au3>
Local $result
DirCreate(@DesktopDir & "\Test\Torré")
$pid = Run(@ComSpec & " /c dir /B ", @DesktopDir & "\Test", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While 1
    $result &= StdoutRead($pid)
    If @error Then ExitLoop
Wend
$array = StringSplit($result, @CRLF, 1)
_ArrayDisplay($array)
Link to comment
Share on other sites

$aArray = _Array_CreateFromString(StdOutRead($Run))
If NOT @Error Then
    ;;Do whatever with the array
EndIf

Func _Array_CreateFromString($sStr)
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   If NOT $sStr Then Return SetError(1);; Empty strings not allowed
   Local $aRtn = StringRegExp($sStr, "(?i)(?m:^)(.*)(?:\v|$)", 3)
   If @Error Then Return SetError(2) ;; RegExp failed
   Return $aRtn
EndFunc    ;<===> _Array_CreateFromString()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just do it like the pros do :)

Only issue is with non-standard characters - any simple fix for this?

Yep:

#include <Array.au3>
#include <Constants.au3>

Local $result

DirCreate(@DesktopDir & "\Test\Torré")
$pid = Run(@ComSpec & " /c dir /B ", @DesktopDir & "\Test", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While 1
    $result &= StdoutRead($pid)
    If @error Then ExitLoop
Wend

$array = StringSplit(StringStripWS($result, 8), @CRLF, 1)
MsgBox("", "String", "Before conversion: " & @CRLF & @CRLF & $array[1])
$output_comspec_oem2ansi = _OEM2ANSI($array[1])
MsgBox("", "String", "After conversion: " & @CRLF & @CRLF & $output_comspec_oem2ansi)


Func _OEM2ANSI($what)
    $ret = DllCall('user32.dll', 'Int', 'OemToChar', 'str', $what, 'str', '')
    Return $ret[2]
EndFunc   ;==>_OEM2ANSI
Edited by cherdeg
Link to comment
Share on other sites

Just do it like the pros do :)

Yep:

#include <Array.au3>
#include <Constants.au3>

Local $result

DirCreate(@DesktopDir & "\Test\Torré")
$pid = Run(@ComSpec & " /c dir /B ", @DesktopDir & "\Test", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While 1
    $result &= StdoutRead($pid)
    If @error Then ExitLoop
Wend

$array = StringSplit(StringStripWS($result, 8), @CRLF, 1)
MsgBox("", "String", "Before conversion: " & @CRLF & @CRLF & $array[1])
$output_comspec_oem2ansi = _OEM2ANSI($array[1])
MsgBox("", "String", "After conversion: " & @CRLF & @CRLF & $output_comspec_oem2ansi)


Func _OEM2ANSI($what)
    $ret = DllCall('user32.dll', 'Int', 'OemToChar', 'str', $what, 'str', '')
    Return $ret[2]
EndFunc   ;==>_OEM2ANSI

Hey Cherdeg,

Kool - works great thanks

;)

Codeflakes

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...