Stephen W. Thomas BizTalk Blog

100% Pure BizTalk
posts - 133, comments - 192, trackbacks - 427

My Links

News

Subscribe to my blog via:


Add to Google

Visit my website at:
www.BizTalkGurus.com

Archives

BizTalk 2004 Samples

BizTalk 2006 Samples

BizTalk Videos

BizTalk White Papers

BizTalkBlogs.com

Great BizTalk Blogs

Other Links

Biztalk 2004 Prepend to SourceFileName Macro 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.

Print | posted on Monday, August 01, 2005 4:35 PM |

Feedback

Gravatar

# re: Biztalk 2004 Prepend to SourceFileName Macro Sample

hi, i have a similiar problem

I'm working with biztak 2004 about 2 years and now i have in problems. I've tried for 2 days and
I got tired of looking on the Web in forums and other pages. Well, I like make this:

I like call from my Pipeline c# code the RECEIVE PORT NAME where the message for input

My code is like this:

Firs, i put a xml named: "example.xml"

namespace Servipag.Proteus.Biztalk.Pipeline
{
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Resources;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Component;
using Microsoft.BizTalk.Messaging;


using System.Xml;

[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[System.Runtime.InteropServices.Guid("9ca04587-ff45-42ea-8250-64cbbb0cab7f")]
[ComponentCategory(CategoryTypes.CATID_Encoder)]
public class especializado : Microsoft.BizTalk.Component.Interop.IComponent, IBaseComponent, IPersistPropertyBag, IComponentUI
{

..... ....

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
{
string contenido;
using (StreamReader reader = new StreamReader(inmsg.BodyPart.Data))
{
contenido = reader.ReadToEnd(); //xml incoming that i read for working with this

}

//Here i work with "contenido", for example i make a file in hard drive, this work but...read down.....

return inmsg;

}

I can work with "contenido", transform and return inmsg, but my problems is that i like Write some parte of the "contenido" in a file in hard drive but THE SAME NAME with the XML INCOMING, ie, "example.xml"

Ideally, there is a code like this:

string name = Port.name();
or
strinn name = Port.macro (%sourcefilename") // like in orchestrations,

I now that with Orchetrations this can be done, put i don't like use this.

Is it possible to do this without orchestrations?

thanks!

Hernaldo G.

PD: sorry for my bad english :)
1/23/2009 9:55 AM | Hernaldo Gonzalez
Gravatar

# re: Biztalk 2004 Prepend to SourceFileName Macro Sample

Hello.

You’ll need to read that file name out of the message context.

Look up Context.Read (if I remember right) in the help guide.

One note, is the value to read may vary based on the Adapter. I.e the SOAP Adapter will not set the ReceviedFileName property. Also, the property will be a full path so you’ll need to parse it to get the file name.

Hope this helps.

Stephen W. Thomas
1/25/2009 11:18 PM | Stephen W. Thomas
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: