Day 11
Sometimes we want to be able to automate tasks with regard to Connect:Direct. On Windows we could do this using batch files (.bat or .cmd), or with an application using the Connect:Direct Windows SDK. My preferred way is to use some form of VBScript and the OLE/COM interface that the Connect:Direct Windows SDK provides.
Start by looking at the example below from the comment "Processing starts here" where you will see the creation of an object of type "CD.Node". If my memory serves me well this is not well documented in the Connect:Direct Windows SDK manual.
C:\> cscript cdvbsdk.vbs
Sometimes we want to be able to automate tasks with regard to Connect:Direct. On Windows we could do this using batch files (.bat or .cmd), or with an application using the Connect:Direct Windows SDK. My preferred way is to use some form of VBScript and the OLE/COM interface that the Connect:Direct Windows SDK provides.
That sounds more complicated that it really is. I'll show an example Windows Script Host (WSH) script that uses the OLE/COM interface to connect to the local node and send/receive files with a remote node.
Start by looking at the example below from the comment "Processing starts here" where you will see the creation of an object of type "CD.Node". If my memory serves me well this is not well documented in the Connect:Direct Windows SDK manual.
Once we have an object that represents our local node in my case called "NICKE" we can make a connection with our credentials. In this example the user name and password are empty strings and it makes the connection based on how the "CD Client Connection Utility" is setup. This utility allows you to define several user credentials and which one is to be used as the default when none are given as in my example below.
The rest of the script then goes on to perform a transfer using the "CDSend" function to a remote node called "CD.OTHER", check for an error and then perform a receive of a file and then another check for an error. Both "CDSend" & "CDRecv" make use of a generic function "CDTransfer" to perform a file transfer with the direction of the transfer passed as a parameter which is then used to generate a dynamic Connect:Direct process which is the submitted using the "Node.Submit" function and then the statistics for the process just submitted and executed are analysed and the highest condition code returned to the caller of the "CDTransfer" function.
At the end of the script the connection with the local node is disconnected and the "Node" object de-allocated.
The script below can either be run by double-clicking directly on the ".vbs" script in the file explorer or by giving the name of the script as a parameter to the "CSCRIPT.EXE" program as follows:
C:\> cscript cdvbsdk.vbs
In the next post I will an alternative method of driving Connect:Direct from VBScript.
' Windows Script Host Sample Connect:Direct Script. ' ' Includes ability to push and pull files to/from the other Node. Dim Node ' Represents the Connect Direct Node we are using. Dim procs ' A collection of process records. Dim proc ' An individual process record. Dim stats ' A collection of statistic records. Dim stat ' An individual statistic record. ' Connect:Direct condition codes values warningConditionCode = 4 errorConditionCode = 8 Function CDTransfer (fromFile, toFile, otherNode, direc) If direct = "PUSH" then localOrRemote = "LOCAL" remoteOrLocal = "REMOTE" else localOrRemote = "REMOTE" remoteOrLocal = "LOCAL" End If ' The process text to be submitted. txtProcess = "TRANSFER 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 set proc = Node.Submit(txtProcess) ' Submit the process ' Get the statistics regarding the process that has now finished. set stats = Node.SelectStats("SELECT STATISTICS PNUMBER=" & proc.ProcessNumber) ' If any step in the process has a condition code greater than "warning" we have a problem. highestConditionCode =0 For Each stat in stats if stat.ConditionCode > highestConditionCode then highestConditionCode = stat.ConditionCode End If Next If highestConditionCode > warningConditionCode then CDTRansfer = highestConditionCode End If End Function Function CDSend ( fromfile, toFile, otherNode ) CDSend = CDTransfer ( fromfile, toFile, otherNode, "PUSH" ) End Function Function CDRecv ( fromfile, toFile, otherNode ) CDRecv = CDTransfer ( fromfile, toFile, otherNode, "PULL" ) End Function ' ------------------------------------------------------------------------------------------ ' ' Processing starts here ' Set Node = CreateObject("CD.Node") ' Make the OLE/COM connection to Connect:Direct Node.Connect "NICKE","","" ' Sign on exitcode = 0 rtc = CDSend ( "c:\temp\input.txt", "c:\temp\output.txt", "CD.OTHER") if rtc > warningConditionCode then wscript.echo "Send Failed" exitcode = exitcode + rtc End If rtc = CDRecv ( "c:\temp\input.txt", "c:\temp\recv.txt", "CD.OTHER") if rtc > warningConditionCode then wscript.echo "Recv Failed" exitcode = exitcode + rtc End If ' You need to disconnect from the Node or you will leave cscript or wscript processes hanging around Node.Disconnect Set Node=Nothing Wscript.Quit(exitcode)