Pages

Solved: BizTalk Copy N Number Files From One Folder to another using .net Component

Tuesday, October 4, 2011
Q: BizTalk Copy N Number  Files From One Folder to another using .net Component

Hi BIZTALK experts!
As I am developing one BIZTALK interface,  I am receiving XML file in Receive port, which contains one element called  File Location, File location contains 'n' no of Files.
 I want to transfer all files First from that location to another location, after verifying All files transferred to Particular location, I want to move the xml file to send port. For this I am creating C# class library, which works fine, And after creating dll and register using Regasm,  I am adding this dll in reference. All works fine.
If I am calling this component in BIZTALK expression editor, but the component
is not working.
Any solution?

Actually , for Transferring 'n' no of Files in particular location is the Required solution , then we can use  FIle adapter.
My situation is, First I am passing first xml, which contains many fields, one element is 'From Location"  which contains many files (n files, 1-> docx, 2. xls, 3.txt).   In biztalk expression editor, I have to transfer these files into 'File send location'.  After moving  these files from 'From Location' to 'Send Location",   after successful transfer of file, I should pass this First receiving  xml to  http send port.
The following code I have given in Biztalk Expression Editor:
StFromLocation = rcv_Msg.FromLocation;
StrToLocation =rcv_Msg.ToLocation;
System.IO.Directory.GetFiles(StrFileLocation);
What should I gave after these commands( like in c#  Any Loop thru command ) can I use in Expression  editor to achieve the result?

Second solution I tried.
I wrote one c# class library, for moving these files from FromLocation to ToLocation, which works fine. And after registering this using Regasm, I am calling following code in Biztalk expression editor.  But  this interface , I am getting the error  " Published message could mnot be routed".
For calling C# dll I gave the following commands:

FileTransferComponent.FileTransferClass.FileTransfer("D:\\users\\InFolder", "D:\\users\\public\\TestFolder\\OutFolderr");
The following is the part of the  C# class library project :
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileTransferComponent
{
[Serializable]
    public class FileTransferClass

    {
        public static void FileTransfer(string FromLocation, string ToLocation)
        {
                 if (System.IO.Directory.Exists(FromLocation))
                 {
                     string[] files = System.IO.Directory.GetFiles(FromLocation);                   
                     foreach (string s in files)
                     {
                         fileName = System.IO.Path.GetFileName(s);
                         destFile = System.IO.Path.Combine(targetPath, fileName);
                       //  System.IO.File.Copy(s, destFile, true);
                         System.IO.File.Move(s, destFile);
                     }
                 }
                 else
                 {
                     Console.WriteLine("Source path does not exist!");
                 }
     }
  }
}


Sol:

I tried your code with little declarations missing and it works perfectly.Here is the class file.
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace FileTransferComponent

{ [Serializable]

public class FileTransferClass {

public static void FileTransfer(string FromLocation, string ToLocation)

{ if (System.IO.Directory.Exists(FromLocation))

{ string[] files = System.IO.Directory.GetFiles(FromLocation);

foreach (string s in files)

{ string fileName = System.IO.Path.GetFileName(s);

string destFile = System.IO.Path.Combine(ToLocation, fileName);

System.IO.File.Move(s, destFile);

}

}

else

{ Console.WriteLine("Source path does not exist!"); }}}}


I built the class library and GACed it. Check if your latest classlibrary dll is in GAC.
This was my code in the expression editor.

FileTransferComponent.FileTransferClass.FileTransfer("C:\\File\\In","C:\\File\\Out");


I see the files being moved from In folder to Out folder.
Thanks.

No comments:

Post a Comment

Post Your Comment...