Ask Your Question
0

How to get the stdout result of a Basic "shell" order ?

asked 2016-07-26 00:51:51 +0200

Hervé Dhalluin gravatar image

The external command I want to call by a Basic "shell" order give its result on its "stdout" stream. How to obtain this result as a Basic string-variable, MacOS and Windows ? See : link text for a MS-VisualBasic solution.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2016-08-20 06:07:06 +0200

mark_t gravatar image

Following works on windows 8.1, only a few simple changes from the VBA version linked in your question

REM  *****  BASIC  *****

Option Explicit

Sub TestShellRun

    msgbox ShellRun("cmd.exe /c dir")
End Sub

Public Function ShellRun(sCmd As String) As String

    'Run a shell command, returning the output as a string'

    Dim oShell As Object
    oShell = CreateObject("WScript.Shell")

    'run command'
    Dim oExec As Object
    Dim oOutput As Object

    oExec = oShell.Exec(sCmd)
    oOutput = oExec.StdOut

    'handle the results as they are written to and read from the StdOut object'
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & chr(10)
    Wend

    ShellRun = s

End Function
edit flag offensive delete link more

Comments

How to do the same thing on macOXs

Antonio gravatar imageAntonio ( 2017-06-30 19:23:19 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-07-26 00:51:51 +0200

Seen: 1,214 times

Last updated: Aug 20 '16