-
-
Save tangzhen/f58dea15c62be2101dcc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-IniContent ($filePath) | |
{ | |
$ini = @{} | |
$content = Get-Content $filePath | |
if ( $content -match "^\[(.+)\]" ) { | |
switch -regex -file $FilePath | |
{ | |
"^\[(.+)\]" # Section | |
{ | |
$section = $matches[1] | |
$ini[$section] = @{} | |
$CommentCount = 0 | |
} | |
"^(;.*)$" # Comment | |
{ | |
$value = $matches[1] | |
$CommentCount = $CommentCount + 1 | |
$name = "Comment" + $CommentCount | |
$ini[$section][$name] = $value | |
} | |
"(.+?)\s*=(.*)" # Key | |
{ | |
$name,$value = $matches[1..2] | |
$ini[$section][$name] = $value | |
} | |
} | |
} else { | |
switch -regex -file $FilePath | |
{ | |
"^(#.*)$" # Comment | |
{ | |
$value = $matches[1] | |
$CommentCount = $CommentCount + 1 | |
$name = "Comment" + $CommentCount | |
$ini[$name] = $value | |
} | |
"(.+?)\s*(.*)" # Key | |
{ | |
$name,$value = $matches[1..2] | |
$ini[$name] = $value | |
} | |
} | |
} | |
return $ini | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Out-IniFile($InputObject, $FilePath) | |
{ | |
$outFile = New-Item -ItemType file -Path $Filepath | |
foreach ($i in $InputObject.keys) | |
{ | |
if (!($($InputObject[$i].GetType().Name) -eq "Hashtable")) | |
{ | |
#No Sections | |
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" | |
} else { | |
#Sections | |
Add-Content -Path $outFile -Value "[$i]" | |
Foreach ($j in ($InputObject[$i].keys | Sort-Object)) | |
{ | |
if ($j -match "^Comment[\d]+") { | |
Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" | |
} else { | |
Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" | |
} | |
} | |
Add-Content -Path $outFile -Value "" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment