Shell_CaptureOutput

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > BuiltIn Functions > General functions >

Shell_CaptureOutput

 

Description

 

Execute a command and capture command output if any.

 

Syntax

 

sOutput = Shell_CaptureOutput(sCmdLine, sStartDirectory, lShow, dwMillisecondsWait)

 

Returns

 

String, the output of the called process.

 

Parameters

 

Name

Type

Optional

Meaning

sCmdLine

String

No

The command line to be executed

 

The maximum length of command line string is 32,768 characters.

 

To execute operating system commands use:

"cmd /c " followed by the command.

 

sStartDirectory

String

No

The full path to the current directory for the process. The string can also specify a UNC path.

 

If this parameter is NULL, the new process will have the same current drive and directory as the calling process. (This feature is provided primarily for shells that need to start an application and specify its initial drive and working directory.)

lShow

Number

No

Controls how the window is to be shown. Possible values:

%SW_HIDE

%SW_SHOWNORMAL

%SW_SHOWDEFAULT

dwMillisecondsWait

Number

No

It indicates the maximum number of milliseconds the command will wait till return

 

Remarks

 

The new process runs in the security context of the calling process.

Additional info: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

 

Restrictions

 

By default Shell is a synchronous function.

 

See also

 

OS_Shell

OS_ShellExecute

 

Examples

 

'---Execute an operating system command and capture its output

uses "console"

 

string sCommand

 

'---Loop until command is an empty String

Do

  sCommand = sGetCommand 

 

  if len(sCommand) > 0 Then

    printl shell_captureoutput("cmd /c " & sCommand, "c:\"%SW_hide, 10000)

  Else

    exit do

  end If

Loop

 

printl "---Press a ley to end ---"

WaitKey

 

function sGetCommand() as String

  local sCommand as String

  

  print "Type an operating system command: " in %CCOLOR_FYELLOW

  sCommand = Console_ReadLine

 

  '---Check some allowed commands

  select case ucase$(parse$(sCommand, any " (" & $TAB & $DQ, 1))

    Case "DIR"

    case "HELP"

    CASE "VER"

    CASE "TITLE"

    case ""

    case Else

      sCommand = "VER"

      printl "Command not supported" in %CCOLOR_FLIGHTRED

  end Select

  

  function = sCommand

  

end function