utf-8(BOMなし) から shift-jis への 文字コード 一括変換 スクリプト(Windows) [Windows]
スポンサーリンク
以下のような構成のフォルダーに存在する utf-8 エンコーディングのファイル全て(texta、textb、textc)を shift-jis エンコーディングのファイルに一括変換
C:\from
+- folder1
+- texta
+- textb
+- folder2
+- textc
指定(C:\to)した出力先フォルダーに以下のように変換したファイルを出力
C:\to
+- from
+- folder1
+- texta
+- textb
+- folder2
+- textc
OS は Windows 10 、Powershell のバージョンは 5.1.17763.503(Get-Host コマンドにて確認)
パラメーターチェック、エラー処理、他、色々と適当に割愛
[コマンド書式]
utf8tosjis.bat [変換元フォルダー] [変換先フォルダー]
やりたい事を例とした場合、以下のようにコマンドを起動
utf8tosjis.bat c:\from c:\to
[スクリプト(BATファイル)]
@echo off REM 文字コード変換(utf8 - shift-jis) REM コマンド使用方法: utf8tosjis.bat 変換元フォルダー 変換先フォルダー setlocal set fromdir=%~dpf1 set todir=%~dpf2 if "%fromdir%"=="" ( exit /b 0 ) if "%todir%"=="" ( exit /b 0 ) for /r %fromdir% %%a in (*) do ( call :changeencoding "%%a" %todir% ) exit /b 0 :changeencoding echo 文字コード変換 %~f1 --^> %todir%%~p1%~nx1 call :checkfolder "%todir%%~p1" powershell -nop -c "get-content -encoding utf8 \"%~f1\" | Out-String | %% { [Text.Encoding]::GetEncoding(\"shift_jis\").GetBytes($_) } | set-content -encoding Byte \"%todir%%~p1%~nx1\"" exit /b 0 :checkfolder if not exist "%~dp1" ( echo フォルダー作成 %~dp1 mkdir "%~dp1" ) exit /b 0
[補足]
「powershell -nop ~ | Out-String | %% { ~ } ~」の「%%」について
・Powershell の Foreach-Object スクリプトブロックのエイリアスがパーセント(%)
・BATファイルのパーセント(%)文字のエスケープがパーセント(%)
スポンサーリンク
コメント 0