i have a ini file:
number=1
problem=4
issue=2
test=7
number=2
problem=5
issue=3
test=8
with duplicate keys but different values.
How can I delete every doubled key with powershell?
My final ini file should look like this:
number=1
problem=4
issue=2
test=7
Thank you!
UPDATE: 09/10/2018 (I was in holidays sorry)
I finally made it! It works absolutely perfect, thank you guys and special thanks to @Richard Siddaway
@Theo you are right. I missed my [sections] and I added them and a further part for my second program that should be able to read the ini file
So this is my code:
#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\*.*
This was my ini-file after the merge:
[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
And this is my result:
[C:\mypath\File.ini|SECTION1]
Tester=5
Magnet=2
Problem=
Issue=
[C:\mypath\File.ini|SECTION2]
Progress=0
As you can see it still looks like someone "just pasted together some random snippets" as @Paxz said. I'm finished and totally happy with it because it does what its meant to be, but I well know my script is not very clean neither a good solution. Especially with my outputs :/ maybe someone has hints for me to improve for the next time? Maybe I can somehow hold every output in an variable? And I added an '=' to my sections to be able to use Richards script...