Some projects have a lot of labels and when working with a team of developers, merging label files can be a nightmare provide some challenges. Microsoft Dynamics AX doesn’t like duplicate labels and, unfortunately, identifying all instances of duplicates isn’t a straightforward process. I’ve therefore put a PowerShell script together that identifies duplicate labels.

The PowerShell script will print to screen:

  • The duplicate label name
  • The corresponding line number of the duplicate entry
  • The first instance of the label name

The script is as follows:

Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "Text files (*.txt)| *.txt"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

$kwdlist = New-Object System.Collections.Generic.List[System.Object]
$linelist = New-Object System.Collections.Generic.List[System.Object]

$inputfile = Get-FileName pwd

if(!$inputfile) {
    exit
}

$lineno=0
foreach($line in Get-Content $inputfile) {
    $lineno++
    if(!$line.startswith(";") -And $line -like '*=*') {
        $item = $line.split("=")[0]
        if($kwdlist -ccontains $item) {
            $linenumber = $linelist[$kwdlist.indexof($item)]
            Write-Host "Duplicate value '$item' on line $lineno (original on line $linenumber)"
        }else{
            $kwdlist.add($item)
            $linelist.add($lineno)
        }
    }
}

Simply save the contents to a file (e.g. find_duplicates.ps1) and run the script. A dialog box should pop up asking you to select a label file for processing. Results will appear in the PowerShell window.

Next Post Previous Post