Forum Discussion

HoneyGulati's avatar
HoneyGulati
New Contributor III
7 months ago

Can we merge 2 files using Business Rules

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?

  • aformenti's avatar
    aformenti
    7 months ago

    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

     

  • Henning's avatar
    Henning
    Valued Contributor II

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

  • aformenti's avatar
    aformenti
    Contributor II

    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... 

    • HoneyGulati's avatar
      HoneyGulati
      New Contributor III

      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?

      • aformenti's avatar
        aformenti
        Contributor II

        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