SplitNAVObjects.ps1

#accept a given filename that points to a text export of one or more objects from NAV
#create a directory (or clear it if it already exists) with the same name as the file
#read the contents of the text file and split it between instances of "OBJECT "
#create a separate text file in the new directory for each part of the split, reading
#the object type and ID from the first part of the split text

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$filename
)

#create or clear the appropriate directory
$directory = Split-Path -Path $filename -Parent
$fileLeaf = Split-Path -Path $filename -Leaf
$directory = $directory + "\" + $fileLeaf.Substring(0,$fileLeaf.LastIndexOf("."))

if ([IO.Directory]::Exists($directory))
{
    $childPath = Join-Path -Path $directory -ChildPath "*"
    Remove-Item -Path $childPath -Recurse
}
else
{
    [IO.Directory]::CreateDirectory($directory)
}

#read the contents of the file and split using regex
$fileContent = Get-Content -Path $filename -Raw
$regexExpression = "\nOBJECT "

$objects = [regex]::Split($fileContent,$regexExpression);

Write-Progress -Activity "Writing files"

$objectCount = $objects.Count
$objectNo = 0

#iterate through the collection of text splits, reading the object type and ID and writing them to new files
foreach($object in $objects)
{    
    if ($object.Length -gt 0)
    {
        $objectNo += 1;
        $status = "File {0} of {1}" -f $objectNo, $objectCount
        Write-Progress -Activity "Writing files" -Status $status -PercentComplete (($objectNo / $objectCount) * 100)

        if ($object.Substring(0,6) -ne "OBJECT")
        {
            $object = "OBJECT " + $object
        }
        
        if ($object.Substring(7,1) -eq '[')
        {
            $objectType = $object.Substring(8,$object.IndexOf(" ",9) - 8).Substring(0,3).ToUpper()
        }
        else
        {
            $objectType = $object.Substring(7,$object.IndexOf(" ",8) - 7).Substring(0,3).ToUpper()
        }
        $objectID = $object.Substring($object.IndexOf(" ",8) + 1,$object.IndexOf(" ",$object.IndexOf(" ",8) + 1) - $object.IndexOf(" ",8) - 1)
        $newFileName = $objectType + $objectID + ".TXT"
        $newFileName = Join-Path -Path $directory -ChildPath $newFileName
        
        #for reasons I'm not clear about splitting the objects introduces a singleton carriage return, the below lines gets rid of it
        $object = [regex]::Replace($object, "\r(?!\n)", '')

        #if this is the last object in the file there will be a trailing CRLF (again for reasons I'm not entirely clear about)
        if ($objectNo -eq $objectCount)
        {
            $object = $object.Remove($object.LastIndexOf("}") + 1)
            $object = $object + [Environment]::NewLine
        }

        if (Get-ChildItem $newFileName -ErrorAction SilentlyContinue) { Remove-Item -Path $newFileName }
        Add-Content -Path $newFileName -Value $object
    }
}

Write-Progress -Activity "Writing files" -Completed