How To Prepend to the SourceFileName in BizTalk Sample

Biztalk 2004 provides a variety of built in macros for dynamically naming files.  A full list of macros can be found in the help guide or here. 



Recently, I have been looking at SourceFileName.  I have posted a sample in the past that covers how to name output files inside your Orchestration using the %SourceFileName% macro.  In that sample, I completely override the File.ReceivedFileName property with a new value.  The File.ReceivedFileName property is the data source for the %SourceFileName% macro.



What if I wanted to append or prepend data to the original file name?  When using the File Receive Adapter, the File.ReceivedFileName property looks like this: “c:\somepath\FileName.xml”. 



If you prepend or appended text to this, you will get “PrePend_c:\somepath\FileName.xml” or “c:\somepath\FileName.xml_Append”. 



When the %SourceFileName% macro runs, it looks for the last “\” and truncates the beginning part of the filename.  So, in the Prepend case, the prepended text is completely lost.



How can I prepend or append text to the filename?  Easy, just use a simple static helper class to prep the filename.  The method to prepend text would look like this:



public static string PrePendText(string sReceivedFileName, string sPreppendText)


{


 int nLastIndex = sReceivedFileName.LastIndexOf(@”\”);


 return sPreppendText + sReceivedFileName.Substring(nLastIndex+1);


}



I have put together a sample with three methods; one to prepend text, one to append text, and one to append text and change the file extension. 



Download: Prepend and Append Text to Filename Sample



Setup is easy.  Just deploy the Orchestration and do not forget to GAC the Helper class.

How to Name Output Files Inside An Orchestration

In many cases it can be useful to know the exact name of your output file that will be sent from your Orchestration using the File Adapter.  This can be difficult if you are using the %MessageId%.xml macro to write the file since this it set after the message is sent from the Orchestration. 



Delivery Notification can help you determine if your message was sent successfully but it can not give you the file name.



BizTalk 2004 has two ways to dynamically name your files from inside the Orchestration.  The two ways to accomplish this are either to use a Dynamic Send Port or to use the %SourceFileName% macro on the Send Port.



Dynamic Send Port


Dynamitic Send Ports are powerful and useful if you need to send your files to many different locations on the file system like sometime to C:\data\ and other times c:\root\.  The downside is you need to have all this information inside your message or hard code it in the Orchestration.  So, it can be difficult to change.



Source File Name Macro


This is my preferred approach to Output File Naming.  This does not require your data or the Orchestration to have any idea as to the directory you want to write your file to.  This requires using the %SourceFileName% macro to set the output file name inside the Send Port. 



Don’t want the same name as your input file you say?  Now, here is the trick.  Just change the File.ReceivedFileName property inside the Orchestration to be anything you want!  This can be done by creating a new message and changing the context property.  The code inside a Message Assignment shape would look like this:



// Create a new message


OutMessage = InMessage;



// Set the ReceivedFileName context property


OutMessage(FILE.ReceivedFileName) = “SetInOrch.xml”;



It is not required to demote this value into your message.  So, this method works with the Pass Through Send Pipeline because this context value is used by the File Adapter and not the pipeline.



CRITICAL: The %SourceFileName% macro does not need an additional extension (like .xml or .txt) after it like the %MessageId% macro.



I have put together a simple sample showing both of these types of file naming.  For information on how to run the samples, please see the Read Me file.



DOWLOAD: Sample Naming Output Files