Martijn's blog - E-Commerce, EAI, BizTalk and .NET

2004/06/30

File adapter with regular expressions

I've altered the FILE adapter SDK sample so that you don't have to create a receive location for each file extension you wish to pick up. It accepts a regular expression for the receive location. It's a simple modification, put I was creating lots of receive locations, one for each filetype I had to pick up and got fed up with it. Let me know what you think!

In PickupFilesAndSubmit (DotNetFileReceiverEndpoint.cs), change the lines
DirectoryInfo di = new DirectoryInfo(this.properties.Directory);
FileInfo[] items = di.GetFiles(this.properties.FileMask);


into:

FileInfo[] items = GetFilesRegex(this.properties.Directory, this.properties.FileMask);


and add the function GetFilesRegex() where you want it:

/// <summary>
/// This method retrieves all files from a given folder, matching
/// a regular expression in their filename
/// </summary>
/// <param name="path">The folder to enumerate
/// <param name="regularExpression">the regular expression string for matching the files</param>
/// <returns>an array containing the found filesystem entries</returns>
public static FileInfo[] GetFilesRegex(string path, string regularExpression)
{
 // queue == FIFO
 Queue q = new Queue();

 DirectoryInfo di = new DirectoryInfo(path);

 foreach(FileInfo fi in di.GetFiles())
 {
  // see if we match our regular expression
  if(Regex.Match(fi.Name, regularExpression).Success)
  {
   // we do, enqueue the file
   q.Enqueue(fi);
  }
 }

 // copy the queue back into an array of files
 FileInfo[] fia = new FileInfo[q.Count];
 q.CopyTo(fia, 0);

 return fia;
}


You're done! Now configure the adapter (file mask) with a regular expression: (?i).*\.((dw)|(pd))f

3 Comments:

  • Zeer goeie info, dit ga ik ook implementeren.
    Ik werd helemaal gek van al die recievelocations in mijn nant script te plaatsen.

    By Anonymous Anonymous, at 12:55 PM  

  • This blog is awesome full of usefull information that i was in dire need of.

    By Anonymous Gadgets UK, at 9:54 AM  

  • Thanks you. Very good post.Unless they can offer a really compelling reason for users to come back, it will be the next Bebo, MySpace

    By Anonymous Cannon 10755GF, at 11:22 PM  

Post a Comment

<< Home