5

递归修改当前目录 .cpp、.h、.md 文件为 UTF8 或 UTF8-BOM 格式

 3 years ago
source link: https://www.mycode.net.cn/tools/2879.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

递归修改当前目录 .cpp、.h、.md 文件为 UTF8 或 UTF8-BOM 格式

有些代码目录下的文件格式不同,会导致一些编译错误或者中文出现错误等等问题,下面脚本就是解决这个问题而生的。使用 Autoit3 编译脚本后放到你要转换的目录中,运行脚本会转换所有 .cpp、.h、.md 文件为 UTF8 格式,如果你希望修改成 UTF8-BOM 格式,可以将 $FO_UTF8_NOBOM 修改为 $FO_UTF8

#include <Array.au3> ; Only required to display the arrays
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$aArray = _FileListToArrayRec(@ScriptDir, "*.cpp;*.h;*.md", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
_ArrayDisplay($aArray, "待转换列表.")

For $i = 0 To $aArray[0]
    _Convert2UTF8(@ScriptDir & '\' & $aArray[$i])
Next

Func _Convert2UTF8($sFilePath)
    Local $hInputFile = FileOpen($sFilePath)
    Local $s_Txt = FileRead($hInputFile)
    FileClose($hInputFile)
    Local $hOutputFile = FileOpen($sFilePath, $FO_OVERWRITE + $FO_UTF8_NOBOM)
    FileWrite($hOutputFile, $s_Txt)
    FileClose($hOutputFile)
EndFunc

新增 Python3 版本的脚本,可以通过命令 pythonfile.py -e utf-8-sigpythonfile.py -e utf-8 对当前目录下文件进行转换。pythonfile.py 是你保存的文件名。执行前需要 pip install chardet 安装包。

import argparse
import chardet
import codecs
import os

parser = argparse.ArgumentParser(description='convert encoding')
parser.add_argument('-e', '--encoding', dest='encoding', metavar=None, required=True, action='store', help='encoding')
args = parser.parse_args()

for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        if name.endswith(".cpp") or name.endswith(".h") or name.endswith(".md"):
            content = codecs.open(os.path.join(root, name), 'rb').read()
            encoding = chardet.detect(content)
            if encoding["encoding"] is None:
                continue
            if encoding["encoding"].upper() != args.encoding.upper():
                print(encoding["encoding"], "\t", os.path.join(root, name))
                content = content.decode(encoding["encoding"], 'ignore')
                codecs.open(os.path.join(root, name), 'w', encoding=args.encoding).write(content)

本条目发布于2019年12月10日。属于LanguageTools分类。 ← 英伟达(NVIDIA)支持硬件编解码的设备列表及检查工具 2020 新的开始 →

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

评论

姓名 *

电子邮件 *

站点


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK