Zum Inhalt springen
Split C/AL object files using PowerShell

Split Old fashioned C/AL Object Files

Daniel Gorski
Daniel Gorski 01. Oktober 2022
2 Min. Lesezeit

If you are working with older Dynamics NAV versions you need to handle the old fashioned objects by using fob or txt files.

Powershell is very powerful and if you need to split a big txt file full of objects, you can use or powershell script:

We use Measure-Command in our script to see how long it takes to split about 6.000 object:

TotalSeconds : 57,6412381

This script is using streamwriter and stringbuilder which makes it very fast:

$sourcepath = "D:\\Source\\"
$targetpath = "D:\\Target\\"
Remove-Item $targetpath\*.txt -Recurse -Force
Measure-Command -Expression {
    Get-ChildItem $sourcepath  -Filter *.txt | Foreach-Object {
        $filepath = $_.FullName
        $lines = [IO.File]::OpenText($filepath)
        while ( -not $lines.EndOfStream) {
            $line = $lines.ReadLine()
            if ($line -match "OBJECT (Table|Report|Codeunit|Page|Query|XMLport|Form|Dataport|MenuSuite) (\\d+) (.*)") {
                if ($newfilepath) {
                    $Textstream = [System.IO.StreamWriter]::new($newfilepath, $true)
                    $Textstream.Write($arr.ToString())
                    $Textstream.close()
                    $arr.Clear();
                    $arr = [System.Text.StringBuilder]::new()
                }
                $newfilepath = (Join-Path -Path $targetpath -ChildPath ($Matches[1].ToLower() + '_' + ('{0:d10}' -f [int]$Matches[2]) + $_.Extension));
            }
            if ($line) {
                [void]$arr.Append($line + "`r`n")
            }
        }
        $lines.close()
    }
}

You are going to see two more solutions in our repo by using commands like Out-File or Add-Content.

If you compare all three methods by splitting 25 Objects you will discover how fast streamwriter and streambuilder is:

Add-Content takes:

TotalSeconds : 36,6870116

Out-File takes:

TotalSeconds : 8,2034

StreamWriter takes:

TotalSeconds : 0,3723087

Get the code here:

https://github.com/byndit/UsefulScripts/blob/main/Powershell/Split-Objects.ps1

Have fun!

how-to
c/al development
dynamics nav
c/al
object split
powershell
legacy systems
E-Invoicing-Formate UBL, CII, ZUGFeRD und Factur-X im Vergleich

UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt

UBL, CII, ZUGFeRD oder Factur-X – diese E-Invoicing-Formate werden ständig verwechselt. Wir erklären den Unterschied zwischen reinen XML-Formaten und Hybridform

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
E-Invoicing formats UBL, CII, ZUGFeRD and Factur-X compared

UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained

UBL, CII, ZUGFeRD or Factur-X – these e-invoicing formats get confused all the time. We break down the difference between pure XML formats and hybrid PDF-plus-X

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained