TextFilesHandling/Split-ITIObjectsTxtFile.ps1

<#
.SYNOPSIS
    Splits a .txt file that contains NAV objects and saves each object as an individual file
.DESCRIPTION
    This command reads the .txt file that contains multiple objects, splits the objects and saves each object in a proper subdirectory according to it's type
.EXAMPLE
    Split-ITIObjectsTxtFile -SourcePath ~/MyProject/allObjects.txt -Destination ~/MyProject
.NOTES
    The functions splits the source file into seven subfolders: codeunit, page, query, report, table, xmlport, menusuite
#>

function Split-ITIObjectsTxtFile {
    [CmdletBinding()]
    param (
        # The path to the .txt file containing multiple NAV objects
        [string] $SourcePath = './objects.txt',
        # The path where the seperate files will be saved
        [string] $Destination = './Apps',
        # Specifies if progress bar should be shown
        [boolean] $ShowProgress = $true
    )
    if($ShowProgress) {
        Write-Progress -Activity 'Splitting .txt file' -Status 'Enumerating objects' -PercentComplete 0
    }
    $object = [System.Collections.ArrayList]@();
    $objnumber = 0;
    $lines = (Get-Content -Encoding Oem $SourcePath -Raw) -split [Environment]::NewLine
    $linesNo = $lines.Length
    for ($i = 0; $i -lt $linesNo; $i++) {
        $line = $Lines[$i]
        if ($line -cmatch "^OBJECT\s(Codeunit|Table|Page|Query|Form|XMLport|Report|MenuSuite)\s[0-9]+\s") {
            $objnumber = $object.Add($line+[Environment]::NewLine);
            if ($ShowProgress) {
                Write-Progress -Activity 'Splitting .txt file' -Status 'Enumerating objects' -PercentComplete ($i*100/$linesNo)
            }
        } else {
            if ($line -cmatch ("^\s\s\s\sDate=") -or $line -cmatch ("^\s\s\s\sTime=")) {
                $object[$objnumber] += $line.Substring(0,$line.IndexOf('=') + 1) + ';' + [Environment]::NewLine;
            } elseif (($line -cnotmatch "^\s\s\s\sModified=") -and ($line -cnotmatch "\[LineStart\([0-9]+\)\]")) {
                $object[$objnumber] += $line + [Environment]::NewLine;
            }
        }
    };
    if($ShowProgress) {
        Write-Progress -Activity 'Splitting .txt file' -Status 'Enumerating objects' -PercentComplete 100
    }
    for ($i = 0; $i -lt $object.Count; $i++) {
        if($ShowProgress) {
            Write-Progress -Activity 'Splitting .txt file' -Status 'Splitting objects' -PercentComplete ($i*100/$object.Count)
        }
        $firstLine = ($object[$i] -split [Environment]::NewLine)[0];
        $separatedWords = $firstLine.split(" ");
        $prefix = $separatedWords[1].Substring(0,3).ToUpper()
        $FileName =  $prefix + $separatedWords[2];
        $FileDestination = Join-Path $Destination $SourceSubfoldersMap[$prefix]
        if (-Not(Test-Path $FileDestination)) {
            New-Item $FileDestination -ItemType Directory
        }
        $object[$i].trim() | Out-File (Join-Path $FileDestination ($FileName + ".txt")) -Encoding oem
    }
}
Export-ModuleMember -Function Split-ITIObjectsTxtFile