Monday, June 21, 2010

Driving the Connect:Direct command line

Day 12

So last time I showed you how to use the Connect:Direct SDK OLE/COM interface to Connect:Direct in a VBScript example.

The following example is very similar to the previous one with a few exceptions.

Firstly instead of using the OLE/COM interface we are driving the Connect:Direct command line using the "Direct.exe" program.  We are also running the "Direct.exe" program hidden in the background.

You also won't see any signing on to Connect:Direct as this is taken care of using the "CD Client Connection Utility" to set the default node and user credentials which are picked up when we run the "Direct.exe".

The commands to be sent to the Connect:Direct command line are put in a temporary file and that file is supplied as a parameter when we run the "Direct.exe".

The text of the command that contains the Connect:Direct process is also a little different. In this script you will see it prefixed with the "submit" command, while in the previous script we called the "Submit" function of the "Node" object.

It is important to note that the Connect:Direct process contains the "MAXDELAY" statement if you want the script to wait for the Connect:Direct process to be submitted then run and get the return code.

If you do not have this statement in the process then the process will be submitted and that is all, before control is returned to the VBScript.  As submitting a process hardly ever fails the script could see a zero return code i.e. the submitting the process had no error, but when the Connect:Direct process runs and possibly fails the script will have finished and been unaware of the failure.

So usually for batch scripts you will want to specify the "MAXDELAY" statement so your script gets the result of running the Connect:Direct process and not just submitting it.
You may not want to specify "UNLIMITED", but some reasonable time-out figure such as "01:00:00" for an hour.

For the script below to work it must be placed in the same directory as the "Direct.exe" program.  

This is usually "C:\Program Files\Sterling Commerce\Connect Direct v4.x.00\Common Utilities" .  Where "x" may be different depending on the version you are running.

Both this and the previous script are examples of simple dynamic Connect:Direct process creation.  Later we will look at some more elaborate generation of Connect:Direct processes that apply to a wider range of applications.


' Windows Script Host Sample Connect:Direct Script using the CLI
'
' Includes ability to push and pull files to/from the other Node.

' Declare variables to represent objects

Dim fs, WshShell, tempDir, handle

' Connect:Direct condition codes values 

warningConditionCode = 4
errorConditionCode   = 8

'
' Function to either pull/push a file to/from another node
'                       
Function CDTransfer (pname, fromFile, toFile, otherNode, direc)

        If direct = "PUSH" then
                localOrRemote = "LOCAL"
                remoteOrLocal = "REMOTE"
        else
                localOrRemote = "REMOTE"
                remoteOrLocal = "LOCAL"
        End If

        ' Build the text of the submit command and the process

        txtProcess = "submit"                          + vbCrLf _
                + pname & " PROCESS"                  + vbCrLf _
                + "     MAXDELAY=UNLIMITED"             + vbCrLf _
                + "     REMOTE=" & otherNode            + vbCrLf _
                + "     HOLD = NO"                      + vbCrLf _
                + "STEP01 COPY  FROM ("                 + vbCrLf _
                + "     FILE=" & fromFile               + vbCrLf _
                + "     " & localOrRemote               + vbCrLf _
                + "     TO ("                           + vbCrLf _
                + "     " & remoteOrLocal               + vbCrLf _
                + "     FILE=" & toFile                 + vbCrLf _
                + "     DISP=(RPL))"                    + vbCrLf _
                + "PEND;"                               + vbCrLf _
                + "quit;"                             + vbCrLf

        ' Get temporary file names for input/output with the CLI
        inp=tempDir.path & "\" & fs.GetTempName
        outp=tempDir.path & "\" & fs.GetTempName

        ' Write the input to the CLI in a file
        set handle = fs.OpenTextFile(inp,2,True)
        handle.write txtProcess
        handle.close

        ' Run the CLI hidden, redirecting input and recording the output to a file
        cmd="%comspec% /c Direct.exe -z" & Chr(34) & outp & Chr(34) & " < " & inp
        CDTransfer=WshShell.run(cmd,0,True)

        ' Remove temporary files
        Set handle = fs.GetFile(inp)
        handle.Delete
        Set handle = fs.GetFile(outp)
        handle.Delete
End Function

Function CDSend ( pname, fromfile, toFile, otherNode )
        CDSend = CDTransfer ( pname, fromfile, toFile, otherNode, "PUSH" )
End Function

Function CDRecv ( pname, fromfile, toFile, otherNode )
        CDRecv = CDTransfer ( pname, fromfile, toFile, otherNode, "PULL" )
End Function

' Processing starts here

set fs = CreateObject("Scripting.FileSystemObject")
set tempDir = fs.GetSpecialFolder(2)
Set WshShell = CreateObject("WScript.Shell")

rtc = CDSend ( "SEND","c:\temp\input.txt", "c:\temp\output.txt", "CD.OTHER")

if rtc > warningConditionCode then
        wscript.echo "Send Failed"
        'Return the completon code to the command prompt
        wscript.quit(rtc)
End If

rtc = CDRecv ( "RECEIVE","c:\temp\remote.txt", "c:\temp\recv.txt", "CD.OTHER")

if rtc > warningConditionCode then
        wscript.echo "Recv Failed"
        'Return the completon code to the command prompt
        wscript.quit(rtc)
End If