Make Directory over FTP

102 views
Skip to first unread message

Vishal Bhavsar

unread,
Jan 24, 2012, 6:06:05 AM1/24/12
to InterSystems: Ensemble in Healthcare
Hi All,

I have a requirement where i need to create directory(s) over ftp
dynamically,

Ensemble should check against a given path that the folder exists or
not, if folder does not exist then it should create it. The directory
structure can be nested.

Here is the code i attempted - however it does not seem to work.

s ftp = ##class(%Net.FtpSession).%New()
try
{
//s ..Adapter.FilePath = "/Bhaumik/Team/Viraj/"
s path = "EnsembleTest/"
d ftp.SetDirectory("/Bhaumik/Team/Viraj/")
s tSC = ftp.MakeDirectory(path)
$$$LOGINFO("done" _ tSC)
}
catch err
{
$$$LOGWARNING(err.DisplayString())
}
Quit $$$OK

I am writing this code inside EnsLib.FTP.OutboundAdapter - using it as
outbound adapter to drop the files.

Thanks

Ted Peck

unread,
Jan 24, 2012, 10:09:45 AM1/24/12
to ensemble-in...@googlegroups.com, Vishal Bhavsar
The adapter already maintains an FTP session object for you. As an
example of the sort of code you should write you should look at the
implementation of EnsLib.FTP.OutboundAdapter:Delete(). It is a good
example of the style of coding you should employ. However for
maintainability it's probably better if you write this code in your
BusinessOperation message handler instead of modifying the FTP Adapter.
Or you could subclass the Adapter.
Ted

Vishal Bhavsar

unread,
Jan 24, 2012, 11:35:17 AM1/24/12
to InterSystems: Ensemble in Healthcare
Hi Ted,
I know the method Delete() which delete the folder if already exists.
But, what I want to do is I have to dynamically check the folder
existance. If that folder exists then I have to go inside that folder
and check for the inner folder existance.

i.e. I have ftp folder path=/a/b/c

Now, first I have to check weather folder "/a" exists. If it is then I
will now check for "/a/b" folder. If it is then I will check for the /
a/b/c folder. If "/c" folder doesn't exists then I need to create it
at that location.

I tried to check the existance of folder using below code.

s ftp = ##class(%Net.FtpSession).%New()
d ftp.Connect("servername","username","password","21")
s filePath="/a/b/c"
set filePathArr = $LFS(filePath,"/")
s path=""

For i=2:1:$LL(filePathArr)
{
s folderexists = ftp.GetDirectory(path)
s path= path_"/"
}

Here, the folder "/c" doesn't exists, but the method
ftp.GetDirectory(path) still gives me "1" as the return value.

Please suggest me method to check the existance of folder on ftp.

Thanks,
Vishal.
> > Thanks- Hide quoted text -
>
> - Show quoted text -

Vishal Bhavsar

unread,
Feb 1, 2012, 7:50:41 AM2/1/12
to InterSystems: Ensemble in Healthcare
Hi All.
There is a bit change in the requirement.

I need to create the folder(s) over FTP/FTPS and drop the files in
those folder(s).
To do that, i plan to use the EnsLib.FTP.OutboundAdapter class.

by creating a new instance of that class using objectscript, i can
connect to it programmatically but my question is how do i get the
object reference of %net.ftpsession from that ?
I do not want to create new class of ftpsession and connect it again
because i think that would increase the network traffic.

Alll i want to do is connect to ftp programmatically, check for the
folders, if the folder is not there, create it and drop the file and
disconnect.

Please suggest

Thanks
> > - Show quoted text -- Hide quoted text -

Akash

unread,
Feb 1, 2012, 10:06:44 AM2/1/12
to InterSystems: Ensemble in Healthcare

You can use "Adapter.FTP" property to get the object reference of the
%Net.FtpSession class if you are using Business Operation.

You message is indicating that you trying to use the
EnsLib.FTP.OutboundAdapter in a class other then Business Operation if
yes then replace Adapter with the object name of
EnsLib.FTP.OutboundAdapter you are creating in your class.

To make directories at ftp location you can use
FTP.MakeDirectory(.DirPath) method.

Thanks
Akash
Message has been deleted

Vishal Bhavsar

unread,
Feb 1, 2012, 10:42:22 AM2/1/12
to InterSystems: Ensemble in Healthcare
Hi Akash,
Thanks for your reply.

I tried below code to make dynamic folders and to put the file on the
ftp location.

 Class Utility.Util Extends Ens.Rule.FunctionSet [ Abstract ]
{
Method DynamicFolder(pRequest As EnsLib.HL7.Message) As %Status
{             s Adapter = ##class(EnsLib.FTP.OutboundAdapter).%New()
                s Adapter.SSLConfig ="FTPSSSL"
                s Adapter.FTPServer = "IPAddress"
                s Adapter.FTPPort = "21"
                s Adapter.Credentials="credential"
                s Adapter.FilePath = "/"

               s filePath = "test/testfolder/testfolder2"

                s session = ##class(%Net.FtpSession).%New()
                d
session.Connect("IPAddress","username","password","21")
                s session.SSLConfiguration = "FTPSSSL"

                s tSC = Adapter.Connect(30,0)

                set filePathArr = $LFS(filePath,"/")
                s path=""
                try
                {
                   For i=2:1:$LL(filePathArr)
                   {
                       s path= path_$LIST(filePathArr,i)
                       d session.MakeDirectory(path)     //creates
dynamic folder for me
                       s path= path_"/"
                                }
                       s Adapter.FTP = session
                       if (i= $LL(filePathArr))
                       {
                           s pRequestStream =
##class(%GlobalCharacterStream).%New()
                           d pRequestStream.Rewind()
                           s tSC =
pRequestStream.Write(pRequest.RawContent) // pRequest.RawContent
contains HL7 Messages
                           s Adapter.FilePath = path

  try
                           {
                             d
Adapter.PutStream(fileName,pRequest.RawContent) // Here the file is
created with 0 KB size. ?
                             d session.Logout()
                             d Adapter.FTP.Logout()
                             kill session
                           }
                           catch Error
                           {
                               $$$LOGWARNING("putStreamerr" _
Error.DisplayString())
                           }
                         }
                 }
                 catch err
                 {
                   $$$LOGWARNING("Error "_err.DisplayString())
                }
}
}


Now, My questions are
1.why the file is created with 0 KB size.?
2. I have to provide two time credentials in my code, Is there any
other way to do this?.
Thanks,
Vishal.

Ted Peck

unread,
Feb 1, 2012, 10:42:28 AM2/1/12
to Ensemble-in...@googlegroups.com
I think you are on the wrong track here. You should not try to create a
new adapter instance. Adapters are only supposed to work within a
BusinessOperation message handler method and are supposed to be
instantiated by the Ensemble framework when the BO job starts up.

My advice is to create your FTP directory from within a BO message
handler using the ..Adapter.FTP property that is already available there.

If you need to use FTP features outside of a BO, you should just use
class %Net.FTPSession without involving an adapter at all.

Ted

On 2/1/2012 10:30 AM, Vishal Bhavsar wrote:
> Hi Akash,
> Thanks for your reply.
>
> I tried below code to make dynamic folders and to put the file on the
> ftp location.
>
> Class Utility.Util Extends Ens.Rule.FunctionSet [ Abstract ]
> {
>
> Method DynamicFolder(pRequest As EnsLib.HL7.Message) As %Status
> { s Adapter = ##class(EnsLib.FTP.OutboundAdapter).%New()
> s Adapter.SSLConfig ="FTPSSSL"
> s Adapter.FTPServer = "IPAddress"
> s Adapter.FTPPort = "21"
> s Adapter.Credentials="credential"
> s Adapter.FilePath = "/"
>
> s filePath = "test/testfolder/testfolder2"
>
> s session = ##class(%Net.FtpSession).%New()
> d session.Connect("IPAddress

> ","username","password","21")

> s session.SSLConfiguration = "FTPSSSL"
> s tSC = Adapter.Connect(30,0)

> set filePathArr = $LFS(filePath,"/")
> s path=""

> try
> {
> For i=2:1:$LL(filePathArr)
> {
> s path= path_
> $LIST(filePathArr,i)

Vishal Bhavsar

unread,
Feb 1, 2012, 11:00:12 AM2/1/12
to InterSystems: Ensemble in Healthcare
Hi Ted,
I appreciate your suggestion.
But, I also have to put file on ftp location. Is there any way that I
can put the file using %Net.FTPSession object on ftp location without
creating instance of Adapter in my class

Thanks,
Vishal

Ted Peck

unread,
Feb 1, 2012, 11:29:21 AM2/1/12
to ensemble-in...@googlegroups.com, Vishal Bhavsar
Yes, the %Net.FTPSession object has methods Store() and Append() that
are used for this. You can see how the Adapter uses them by looking at
method EnsLib.FTP.OutboundAdapter:PutStream().

However (not knowing the details of your specific situation) I still
think you should do all this in a BusinessOperation. Use
..Adapter.FTP.MakeDirectory() to make the directory, Set
..Adapter.FilePath to change to the directory and call
..Adapter.PutStream() to populate the file.

Ted

Reply all
Reply to author
Forward
0 new messages