UpgradeTools/Copy-ModifiedCALFiles.ps1

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

$TargetFolderName = 'ModifiedObjects'
$filename = $filename.Replace("`"","") #remove any quotation marks

if ((Get-Item $filename) -is [System.IO.DirectoryInfo])
{
    ##is a directory
    $TargetDirectory = Join-Path -Path $filename -ChildPath $TargetFolderName

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


    $objects = (Get-ChildItem -path $filename -Include *.txt -Recurse | Get-Content -Raw)

}
else
{
    # We got a file going on
    $TargetDirectory = Join-Path -Path (Split-Path -Path $filename -Parent) -ChildPath $TargetFolderName

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

    #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

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 the time is set to 12:00:00 and there is no 'modified' entry, sku
        if (
            ($object.Substring($object.IndexOf("Time=")+5,8) -eq '12:00:00') -AND 
            ($object.IndexOf("Modified=") -eq '-1') 
            )
        {
            continue
        }


        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 $TargetDirectory -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
        }
}