删除双键 ini 文件 powershell

delete double keys ini-file powershell

我有一个 ini 文件:

number=1
problem=4
issue=2
test=7

number=2
problem=5
issue=3
test=8

键重复但值不同。

如何使用 powershell 删除所有双键?

我的最终 ini 文件应该如下所示:

number=1
problem=4
issue=2
test=7

谢谢!

更新:2018 年 9 月 10 日(对不起,我在休假)

我终于成功了!它绝对完美,谢谢你们,特别感谢@Richard Siddaway

@Theo 你是对的。我错过了我的 [sections] 并且我添加了它们以及我的第二个程序的进一步部分应该能够读取 ini 文件

所以这是我的代码:

#merge two files
$files = Get-ChildItem "D:\all"
foreach ($file in $files) {

$name = dir $file.FullName | select -ExpandProperty Name
$ReferenceObject = Get-Content D:\File.ini
$DifferenceObject = Get-Content $file.FullName
Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -PassThru | Out-File ('D:\output\' + $name)

# filter Date an Number
$fileContent = Get-Content ('D:\output\' + $name)
# iterate over lines
foreach($line in $fileContent) {
    #add "=" to [section]
    if($line.StartsWith('[')) {
    $newline = ($line + '=')
    $newline | Add-Content ('D:\output2\' + $name)
    }
  # filter lines beginning with 'Date, ...'
    elseif((-not $line.StartsWith('Date')) -and (-not $line.StartsWith('Number')) -and (-not $line.StartsWith('Date2'))) {
    # output lines to new file
    $line | Add-Content ('D:\output2\' + $name)
  }
}

#remove duplicates
$fileContent = Get-Content ('D:\output2\' + $name)
$unqkeys = [ordered]@{}
foreach ($line in $fileContent){

    if ($line -ne '') {
    $a = $line -split '='

    if (-not $unqkeys[$($a[0].Trim())]){
      $unqkeys += @{$a[0].Trim() = $a[1].Trim()}
    }
  }
}
$unqkeys.GetEnumerator() | 
foreach {
   Out-File -FilePath ('D:\output3\' + $name) -InputObject "$($_.key)=$($_.value)" -Append 
}


# add file location to [section]
$fileContent = Get-Content ('D:\output3\' + $name)
foreach($line in $fileContent) {
  # filter for section
  if($line.StartsWith('[')) {
    # output lines to new file 
    $newline = $line -replace "[[]", "|" -replace "]=", "]"
    $linemodif = ("[C:\mypath\File.ini" + $newline)
    $linemodif | Add-Content ('D:\output4\' + $name)
  }else {
    $line | Add-Content ('D:\output4\' + $name)
  }
}
}

#Move ouput to solution
Get-ChildItem -Path "D:\output4\*.*" -Recurse | Move-Item -Destination "D:\solution" -force

#delete every single output
Remove-Item -Path D:\output\*.*
Remove-Item -Path D:\output2\*.*
Remove-Item -Path D:\output3\*.*
Remove-Item -Path D:\output4\*.*

这是我合并后的 ini 文件:

[SECTION1]
Tester=5
Magnet=2
Number=3459353484
Date=01/01/2018 11:00:00
Problem=
Issue=
[SECTION2]
Progress=0
Tester=4
Magnet=1
Number=0
Date=01/01/1999

这是我的结果:

[C:\mypath\File.ini|SECTION1]
Tester=5
Magnet=2
Problem=
Issue=
[C:\mypath\File.ini|SECTION2]
Progress=0

如您所见,它看起来仍然像@Paxz 所说的"just pasted together some random snippets"。我已经完成并且对它非常满意,因为它完成了它的本意,但我很清楚我的脚本不是很干净,也不是一个好的解决方案。特别是我的输出:/也许有人提示我下次要改进?也许我可以以某种方式将每个输出保存在一个变量中?我在我的部分中添加了一个“=”以便能够使用 Richards 脚本...

试试这个

if (Test-Path -Path c:\test\new.ini) {
  Remove-Item -Path c:\test\new.ini
}

$unqkeys = [ordered]@{}


$lines = Get-Content -Path C:\test\t1.ini 
foreach ($line in $lines){
  if ($line -ne '') {
    $a = $line -split '='

    if (-not $unqkeys[$($a[0].Trim())]){
      $unqkeys += @{$a[0].Trim() = $a[1].Trim()}
    }
  }
}

$unqkeys.GetEnumerator() | 
foreach {
   Out-File -FilePath C:\test\new.ini -InputObject "$($_.key)=$($_.value)" -Append 
}


Get-Content -Path C:\test\new.ini

我正在使用有序散列 table 来保存唯一键 - 在文件中找到的第一个。最后将数据写回一个新文件。您应该可以修改它以满足您的需要