Skip to main content
June 25, 2024
Solved

Can we merge 2 files using Business Rules

  • June 25, 2024
  • 2 replies
  • 0 views

Hi,

I have a requirement to export 2 files and then merge it into single file. While the export can be done via OOTB DM step, I wonder if we have any api or default function which can be used to merge the 2 exported files?

Best answer by Henning

I see, thanks! Just throwing in a suggestion here, since you will be using a business rule, why not creating a single export file right in that rule rather than relying on the DM steps to create two files first just to merge them later on?

aformenti and FredLucas provided some great snippets here:

Solved: Export data using a Method Query - OneStream Community (onestreamsoftware.com)

2 replies

June 25, 2024

Hi, what kind of files are we talking about? Are they using the same format? Are you talking about data extracts, reports, books,...? 

June 25, 2024

Its a Data exported files (same format) from DM step (export data).

I need some help in writing a rule to merge these 2 files into single file

HenningAnswer
June 25, 2024

I see, thanks! Just throwing in a suggestion here, since you will be using a business rule, why not creating a single export file right in that rule rather than relying on the DM steps to create two files first just to merge them later on?

aformenti and FredLucas provided some great snippets here:

Solved: Export data using a Method Query - OneStream Community (onestreamsoftware.com)

June 25, 2024

HI HoneyGulati,

If you talking about Data Files extracted with a DM Step, you could add a third step to run an Extender Business Rule, pick them up from the Data Management folder and merge them both into a single file. I don't have an specific example of how to exactly do that. If you don't know, I could send some snippets for you to work it out... 

June 25, 2024

Hi aformenti Thanks. Yes, that is what exactly am looking for.

Can you share some snippet which would be helpful for me to build a rule for merging 2 data exported file from DM steps?

June 25, 2024

Sure, no problem. 

Following some Api's examples on how to get/manage application files/folders:

				' Declare stringbuilder
				Dim sb As New Text.StringBuilder
				
				' Get FolderName
				Dim folderFullName As String = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, Path.Combine("Documents/Users", StringHelper.RemoveWhiteSpace(si.UserName))).XFFolder.FullName

				' Get All Files In folder
				Dim contentFileExtensionFilters As New List(Of String)({"csv","txt"})
				Dim objList As List(Of XFFileInfoEx) = BRApi.FileSystem.GetFilesInFolder(si, FileSystemLocation.ApplicationDatabase, folderFullName, XFFileType.All, contentFileExtensionFilters)
				objList.ForEach(Sub(lambda) sb.AppendLine(String.Format("objList -> {0}", lambda.XFFileInfo.FullName)))	
				
				' Create XFFolderEx
				Dim objXFFolderEx As XFFolderEx = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, folderFullName)
				sb.AppendLine(String.Format("XFFolder.FullName -> {0}", objXFFolderEx.XFFolder.FullName))

				' Get File
				Dim objXFFileEx As XFFileEx = BRApi.FileSystem.GetFile(si, FileSystemLocation.ApplicationDatabase, String.Format("{0}/{1}", folderFullName, "Test.csv"), True, True)
				sb.AppendLine(String.Format("XFFile.ContentFileBytes.Length -> {0}", objXFFileEx.XFFile.ContentFileBytes.Length))	
				
				' Retrieve File As Bytes()
				Dim fileBytes As Byte() = objXFFileEx.XFFile.ContentFileBytes()
				
				'Connect to Application DB
				Using dbConnApp As DBConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)
				   Dim dbFileInfo As New XFFileInfo(FileSystemLocation.ApplicationDatabase, "TestNew.csv", folderFullName, XFFileType.Unknown)
				   dbFileInfo.ContentFileContainsData = True
				   dbFileInfo.ContentFileExtension = dbFileInfo.Extension
				   Dim dbFile As New XFFile(dbFileInfo, String.Empty, fileBytes)
				   ' Insert File
				   BRApi.FileSystem.InsertOrUpdateFile(si, dbFile)
				End Using				
				
				' Log Result -> Throw Error
				Throw New XFException(Convert.ToString(sb))	

Once you have the two files as byte(), you can do something like (I haven't tested this):

Dim bytesful() As Byte = bytes.Concat(bytescrc).ToArray()

Let me know if you have any problems, happy to assist further.

 Best, 

Albert