KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to do console redirection when using LAUNCH EXTERNAL PROCESS
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Win
Published On: November 12, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Here is a quick tip for using console redirection (redirecting the output of a command) when launching a command with LAUNCH EXTERNAL PROCESS.

Suppose you wanted to issue a command with console redirection; such as a ping:

LAUNCH EXTERNAL PROCESS("ping server > test.txt";$input;$output;$error)

The above will not work with LAUNCH EXTERNAL PROCESS.

However the command runs fine without the console redirection (i.e. "ping server").
LAUNCH EXTERNAL PROCESS("ping server";$input;$output;$error)


There are two general approaches for how to accomplish this:

  1. Prefix your command with "cmd /C " like this:
    LAUNCH EXTERNAL PROCESS("cmd /C ping server > test.txt";$input;$output;$error)

  2. Build a wrapper file (bat file on windows) and then launch that using LAUNCH EXTERNAL PROCESS.

Detailed Examples:


  • Approach 1:

    C_TEXT($file)
    C_TEXT($fileContents)
    C_TEXT($input)
    C_TEXT($output)
    C_TEXT($error)
    C_TEXT($curDir)
    C_TEXT($serverIP)
    C_LONGINT($wait;$count;$delay)

    $curDir:=Get 4D folder(Database Folder)

    $serverIP:="your_server_ip"

    $wait:=500 ` maximum time to wait for a response (in miliseconds)
    $count:=10 ` total number of iterations to try (in miliseconds)
    $delay:=(($wait*$count)/1000)*60 ` amount of time to wait for command to finish

    $resultsFile:="results.txt"

    $file:="ping -n "+String($count)+" -w "+String($wait)+" "+$serverIP
    $file:=$file+" > \""+$curDir+$resultsFile+"\""

    SET ENVIRONMENT VARIABLE("_4D_OPTION_CURRENT_DIRECTORY";$curDir)
    SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
    SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")

    LAUNCH EXTERNAL PROCESS("cmd /C "+$file;$input;$output;$error)

    DELAY PROCESS(Current process;($delay+200))` an extra 200 tick delay for good measure

    SHOW ON DISK($resultsFile;*)

  • Approach 2:

    We can work around the LAUNCH EXTERNAL PROCESS problem by programmatically creating a bat file to run that does the redirection inside of it and then use LAUNCH EXTERNAL PROCESS to launch the bat file. Please see the following sample code:

    C_TEXT($file)
    C_TEXT($fileContents)
    C_TEXT($input)
    C_TEXT($output)
    C_TEXT($error)
    C_TEXT($curDir)
    C_TEXT($serverIP)
    C_LONGINT($wait;$count;$delay)

    $curDir:=Get 4D folder(4D Client Database Folder )

    $serverIP:="your_server_ip"

    $wait:=500 ` maximum time to wait for a response (in miliseconds)
    $count:=10 ` total number of iterations to try (in miliseconds)
    $delay:=(($wait*$count)/1000)*60 ` amount of time to wait for command to finish
    $resultsFile:="results.txt"

    $fileContents:="ping -n "+String($count)+" -w "+String($wait)+" "+$serverIP
    $fileContents:=$fileContents+" > \""+$curDir+$resultsFile+"\""
    $fileContents:=$fileContents+Char(13)+Char(10)
    $fileContents:=$fileContents+"exit"

    $file:="tempPingFile.bat"

    C_TIME($vhDoc)
    $vhDoc:=Create document($curDir+$file)
    If (OK=1)
     SEND PACKET($vhDoc;$fileContents)
     CLOSE DOCUMENT($vhDoc)


     SET ENVIRONMENT VARIABLE("_4D_OPTION_CURRENT_DIRECTORY";$curDir)
     SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
     SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")

     LAUNCH EXTERNAL PROCESS("cmd /C "+$file;$input;$output;$error)

     DELAY PROCESS(Current process;($delay+200))` an extra 200 tick delay

     DELETE DOCUMENT($file)

     SHOW ON DISK($resultsFile;*)

    Else

     ` error creating temp bat file

    End if


    You may need to play around with the timing in the delay in order to allow ample time for the command to finish; but the above gives an example of how this can be achieved.