06-25-2024 06:52 AM
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?
Solved! Go to Solution.
06-25-2024 07:19 AM
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)
06-25-2024 07:41 AM
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
06-25-2024 07:02 AM
Hi, what kind of files are we talking about? Are they using the same format? Are you talking about data extracts, reports, books,...?
06-25-2024 07:11 AM
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
06-25-2024 07:19 AM
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)
06-25-2024 07:07 AM - edited 06-25-2024 07:11 AM
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...
06-25-2024 07:10 AM
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?
06-25-2024 07:41 AM
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