txt(CRLF, sjis)をjson(LF, utf8)に変換するPowershell

カテゴリ : Powershell
公開日 : 2021/07/06 更新日 : 2021/07/06

環境

  • Windows 10 Pro 64bit
  • PowerShell 7.2.0-preview.3

コード

【】は各自環境の値で置換

$targetDir = "【txtファイル保存先のパス】"
$outputDirName = "dest"
$outputDir = $targetDir + "\" + $outputDirName

if(-not(Test-Path "${outputDir}")) {
    mkdir "${outputDir}"
}

cd "${targetDir}"
$jsonFiles = Get-ChildItem -Name -File

foreach($jsonFile in $jsonFiles) {
    '{"text":"' + ((Get-Content $jsonFile -Encoding oem) -join "`n") + '"}' `
    | % { [Text.Encoding]::UTF8.GetBytes($_) } `
    | Set-Content -Path (".\${outputDirName}\" + $jsonFile) -AsByteStream -Encoding UTF8
}

cd "${outputDirName}"
Get-ChildItem -File | Rename-Item -NewName { $_ -replace "\.txt$", ".json" }

Share

Blogger : rice

Twitter GitHub

SAP
DOP