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

10 comments:

Vyom said...

When i try to run this script i am getting Send Failed.

I guess there is logging problem. What can i do , to supply ID/Pass in this code?

Nick Evans said...

You can use the Client Connection Utility in the Connect:Direct start menu to configure the user name and password you want to use. Refer to the Connect:Direct Windows System Guide that comes with the installation on the discs or download from the Sterling Support on Demand website.

Michele said...

Thank you so much for this blog, it's very interesting!

deepak said...

Hi Nick,

You have written a very good blog about connect direct
that’s very helpful to me to have more information on connect direct.

I am facing on problem on the connect: direct if you can sort out that problem I would be grateful to you
Let me explain my problem,

In my project we are using direct.exe to send file to mainframe server and we have schedule on bat file on the our window server which having following information,

"C:\Program Files\Sterling Commerce\Connect Direct v4.1.00\Common Utilities\direct.exe" Nodname nodeusername nodepassword < "C:\Freedom Application\go_legosets\go_legosets\Unload\cd\cmdrat.txt" > "C:\Freedom Application\go_legosets\go_legosets\Unload\cd\lograt.txt"

cmdrat.txt having information about script

There is no matter user is log in or not

So now we have requirements to be removed user name and password from bat file.
Initially what I did as you mention in your blog created node and put user name and password over there.

So my question is that will it work if no user log in window server as CD client connection utility settings only for user specific.
If not how can I manage same thing.
Is there any way to have node with user name and password on the window server and have get that node value while user is not login .
my task will work if no user is log in window server machine.

Please suggest .

Thanks in Advance.

deepak said...
This comment has been removed by a blog administrator.
Unknown said...

Hi Nick,

There are two nodes Node A and Node B on different servers.
I am configuring a vbs script on Node B and making a operation "Receive" from source Node A and the files are successfully pulled to Node B. But how to archive the files on Node A. Please assist if you have any idea.

Unknown said...

Hi Nick - this is an excellent source of info. Could I pick your brains - I have a simple windows batch script invoked by CD File Agent - from the batch script I'd like to invoke a CD process to copy the file but pass into the process the 'toname' and 'fromname' - have been using the DIRECT < piping exe but not sure it's working as don't see anything submitting ? Any help appreciated !

Nick Evans said...

Why not get FileAgent to submit the Connect:Direct process for you by using a template Connect:Direct process file and passing the file names from FileAgent to the template process?

Have a look at http://nick-evans-1965.blogspot.co.uk/2010/04/fileagent-rules-of-game.html

Unknown said...

Hi Nick, Playing with integrating another MFT Product to pass files onto Connect:direct on windows...

The product allows me to run an application and pass a filename as a vairable.

I would like, for example, to be able to run direct.exe (or similar) and then a variable which is the process name and then another variable whcih is the file to be sent...

Is this possible? is direct.exe the correct app and is this as simple as calling direct.exe - ?

Any guidance would be appreciated.

Nick Evans said...

Mark have a look at the Windows batch script on one of my other posts:

http://nick-evans-1965.blogspot.co.uk/2011/09/nostalgia-not.html#more

I think you will find it does pretty much what you wanted.