Skip to content

Instantly share code, notes, and snippets.

@dannyso16
Created September 19, 2022 12:26
Show Gist options
  • Save dannyso16/956ac174fefc88f2a8af37016ee8de42 to your computer and use it in GitHub Desktop.
Save dannyso16/956ac174fefc88f2a8af37016ee8de42 to your computer and use it in GitHub Desktop.
テンプレファイルの一部を置換したファイルを、パラメータ違いで複数出力するPowershell
# 各種設定値
$SOURCE_FILE = "file.txt" # 置換前ファイル
$SETTING = "param.csv" # 置換のハッシュテーブル。各行ごとに新規ファイル作成
$ENCODING = "UTF8"
# 初期処理
$CurrentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $CurrentDir
# メイン処理
$csvFile = Import-Csv -Encoding $ENCODING -Delimiter "," -Path $SETTING
Write-Output $csvFile | Format-Table
$i=1 # 連番ファイル名
foreach($line in $csvFile) {
 # FROM の文字列を TO に変換
$FROM1 = $line.FROM1
$TO1 = $line.TO1
$FROM2 = $line.FROM2
$TO2 = $line.TO2
(Get-Content $SOURCE_FILE -Encoding $ENCODING) | `
Foreach-Object {
$_ -replace $FROM1, $TO1 `
-replace $FROM2, $TO2 `
} |
Set-Content ( "{0:0000}" -f $i) -Encoding $ENCODING
$i++
}
Pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment