32
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数ファイルの文字コードを判定して一覧出力するサクラエディタのマクロ

Last updated at Posted at 2017-02-11

概要

フォルダ内に大量のテキストファイルがあり、それらの文字コードを一括で調べる必要があったので、ファイルの文字コードを判定して一覧として出力するサクラエディタのマクロを作ってみた。

マクロのコード

sakuramacro-check-charcode.js

var TARGET_FOLDER = "C:\\Test";
var FILE_PATTERN = /\.txt$/;

main();

function main() {
    var shell = new ActiveXObject("WScript.Shell");
    var resultFileFolder = shell.ExpandEnvironmentStrings("%TEMP%");
    var resultFileName = "Result_" + formatYyyymmddhhmmss(new Date) + ".txt";
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var resultTextStream = fso.GetFolder(resultFileFolder).CreateTextFile(resultFileName);
    for (var filePaths = new Enumerator(getFilePathsRecursive(fso.GetFolder(TARGET_FOLDER), FILE_PATTERN)) ; !filePaths.atEnd() ; filePaths.moveNext()) {
        resultTextStream.WriteLine(getFileCharCode(filePaths.item()));
    }    
    resultTextStream.Close();
    Editor.FileOpen(resultFileFolder + "\\" + resultFileName);
}

function formatYyyymmddhhmmss(date) {
    var ret = "";
    ret += date.getFullYear();
    ret += ("0" + (date.getMonth() + 1)).slice(-2);
    ret += ("0" + date.getDate()).slice(-2);
    ret += ("0" + date.getHours()).slice(-2);
    ret += ("0" + date.getMinutes()).slice(-2);
    ret += ("0" + date.getSeconds()).slice(-2);
    return ret;
}

function getFilePathsRecursive(folder, pattern) {
    var array = [];
    for (var files = new Enumerator(folder.Files) ; !files.atEnd() ; files.moveNext()) {
        var file = files.item();
        if (pattern.test(file.Name)) {
            array.push(folder.Path + "\\" + file.Name);
        }
    }
    for (var subFolders = new Enumerator(folder.SubFolders) ; !subFolders.atEnd() ; subFolders.moveNext()) {
        var newArray = array.concat(getFilePathsRecursive(subFolders.item(), pattern));
        array = newArray;
    }
    return array;
}

function getFileCharCode(filePath) {
    var charCode = "";
    var stream = new ActiveXObject("ADODB.Stream");
    stream.Open();
    stream.Type = 1;
    stream.LoadFromFile(filePath);
    if (stream.Size > 1) {
        stream.Position = 0;
        switch (bytes2hex(stream.Read(2))) {
            case "feff":
                charCode = "UTF-16 BOM有(BE)";
                break;
            case "fffe":
                charCode = "UTF-16 BOM有(LE)";
                break;
        }
    }
    if (charCode === "" && stream.Size > 2) {
        stream.Position = 0;
        switch (bytes2hex(stream.Read(3))) {
            case "2b2f76":
                charCode = "UTF-7 BOM有";
                break;
            case "efbbbf":
                charCode = "UTF-8 BOM有";
                break;
        }
    }
    stream.Close();
    if (charCode === "") {
        Editor.FileOpen(filePath);
        Editor.Sleep(20);
        switch (Editor.GetCharCode()) {
            case 0:
                charCode = "SJIS";
                break;
            case 1:
                charCode = "JIS";
                break;
            case 2:
                charCode = "EUC";
                break;
            case 3:
                charCode = "UTF-16 BOM無";
                break;
            case 4:
                charCode = "UTF-8 BOM無";
                break;
            case 5:
                charCode = "UTF-7 BOM無";
                break;
            case 6:
                charCode = "UTF-16BE";
                break;
            case 7:
                charCode = "CESU-8";
                break;
            case 8:
                charCode = "Latin1";
                break;
            case 90:
                charCode = "CP_ACP";
                break;
            case 91:
                charCode = "CP_OEM";
                break;
            default:
                charCode = "Unknown";
                break;
        }
        Editor.FileClose();
    }
    return filePath + "\t" + charCode;
}

function bytes2hex(bytes) {
    var doc = new ActiveXObject("Msxml2.DOMDocument");
    var element = doc.createElement("hex");
    element.dataType = "bin.hex";
    element.nodeTypedValue = bytes;
    var hex = element.text;
    return hex;
}

使い方

  1. 上記のコードをコピーしてテキストに貼り付け、拡張子「.js」で保存する。
  • 「TARGET_FOLDER」と「FILE_PATTERN」は適宜書き換えること。
  1. サクラエディタを起動する。
  • サクラエディタのウィンドウが複数開かれているとうまく動作しないので、1つ以外はすべて閉じておくこと。
  1. メニューの「ツール」→「名前を指定してマクロ実行」をクリックする。
    picture01.png
  2. 1で保存したマクロのファイルを選択し、「開く」をクリックする。
  3. すべての対象ファイルの文字コード判定が終わると、一覧が表示される。
    picture02_3.png
32
21
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?