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

6 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home