# 2023/07/26 Structured-TextからCSVへの変換スクリプト param ( [string]$StructuredText, [string]$ExportCSV ) if (($StructuredText.Length -eq 0) -or -not (Test-Path -Path $StructuredText)) { Write-Host "File not found: $StructuredText" exit } $header = @() $name2num = @{} $data = @() Get-Content -Path $StructuredText | ForEach-Object { $line = $_ # 拡張タグ処理 if ($line -match "^#:([\w\$]+)\s*(\w+)") { $tag = $Matches[1] $key = $Matches[2] # Key定義 if (($tag -eq "define") -and -not ($name2num.ContainsKey(($key)))) { $name2num.Add($key,($header.Length)) $header += $key } # コメントアウト処理 } elseif ($line -match "^#") { continue # レコード区切り処理 } elseif (($line -match "^$") -or ($line -match "^\.$")) { if ($data.Length -gt 0) { (($data -join ',') | ConvertFrom-Csv -Header $header) $data.Clear() } } # Structured-Textの解析 if ($line -match "^([\w\$]+): *(.*)$") { $key = $Matches[1] $value = $Matches[2] if ($value -match ",") { $value = (-join ('"', $value, '"')) } if ($name2num.ContainsKey($key)) { $data[($name2num.$key)] = $value } else { $name2num.Add($key, ($header.Length)) if ($data.Length -le $header.Length) { $data += $value } else { $data[($name2num.$key)] = $value } $header += $key } } } | Export-Csv -Path $ExportCSV -Encoding Default -NoTypeInformation Get-Item -Path $ExportCSV