把后缀是.C,.H,.CPP文本都转成ANSI格式文本的BAT处理,解决中文是乱码问题。

 编码检测功能:
        添加了 Test-FileEncoding 函数来检测文件编码
        首先检查 BOM 标记识别带 BOM 的 UTF-8/16/32
        然后检查非 ASCII 字符,并分析其是否符合 UTF-8 编码模式
        最后区分 ASCII/ANSI 和 UTF-8 无 BOM 编码
差异化处理:
        对于识别为 UTF-8 的文件(带或不带 BOM),执行编码转换
        对于识别为 ANSI 或 ASCII 的文件,直接复制不进行转换
        对于无法识别的编码,给出警告并跳过处理
用户反馈:
        增加了彩色输出,提供更清晰的处理状态信息
        处理完成后显示结果文件夹位置

<# :
cls
@echo off
rem 智能检测并转换c/h文件编码
set #=File Encoding Converter&set @=GitHub&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "$args=@('%~dp0');Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default)))"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>

param([string]$path = $PWD)

# 创建结果文件夹
$newFolder = Join-Path $path "result"
if (-not (Test-Path -LiteralPath $newFolder)) {
    New-Item -ItemType Directory -Path $newFolder | Out-Null
}

# 获取所有.c和.h文件
$files = Get-ChildItem -LiteralPath $path | Where-Object {
    ($_.Extension -eq '.c' -or $_.Extension -eq '.h' -or $_.Extension -eq '.cpp') -and 
    ($_ -is [System.IO.FileInfo])
}

# 检测文件编码的函数
function Test-FileEncoding {
    param (
        [Parameter(Mandatory = $true)]
        [string]$FilePath
    )
    
    $bytes = [System.IO.File]::ReadAllBytes($FilePath)
    
    # 检查BOM标记
    if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
        return "UTF8-BOM"
    }
    elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE) {
        return "UTF16-LE"
    }
    elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFE -and $bytes[1] -eq 0xFF) {
        return "UTF16-BE"
    }
    elseif ($bytes.Length -ge 4 -and $bytes[0] -eq 0x00 -and $bytes[1] -eq 0x00 -and $bytes[2] -eq 0xFE -and $bytes[3] -eq 0xFF) {
        return "UTF32"
    }
    
    # 检查是否有非ANSI字符(ASCII范围外的字符)
    for ($i = 0; $i -lt $bytes.Length; $i++) {
        if ($bytes[$i] -gt 127) {
            # 进一步检查是否符合UTF-8编码模式
            if ($i + 1 -lt $bytes.Length -and 
                (($bytes[$i] -band 0xE0) -eq 0xC0 -and ($bytes[$i+1] -band 0xC0) -eq 0x80) -or
                ($i + 2 -lt $bytes.Length -and 
                 ($bytes[$i] -band 0xF0) -eq 0xE0 -and ($bytes[$i+1] -band 0xC0) -eq 0x80 -and ($bytes[$i+2] -band 0xC0) -eq 0x80) -or
                ($i + 3 -lt $bytes.Length -and 
                 ($bytes[$i] -band 0xF8) -eq 0xF0 -and ($bytes[$i+1] -band 0xC0) -eq 0x80 -and ($bytes[$i+2] -band 0xC0) -eq 0x80 -and ($bytes[$i+3] -band 0xC0) -eq 0x80)) {
                return "UTF8"
            }
            else {
                # 非ASCII且不符合UTF-8模式,可能是ANSI
                return "ANSI"
            }
        }
    }
    
    # 所有字符都是ASCII范围内,可能是ANSI或UTF-8无BOM
    return "ASCII/ANSI"
}

# 处理每个文件
foreach ($file in $files) {
    Write-Host "处理文件: $($file.Name)" -ForegroundColor Cyan
    
    $encoding = Test-FileEncoding -FilePath $file.FullName
    $newFile = Join-Path $newFolder $file.Name
    
    switch ($encoding) {
        "UTF8-BOM" {
            Write-Host "  编码: UTF-8 (带BOM) - 转换为ANSI" -ForegroundColor Yellow
            $content = [System.IO.File]::ReadAllText($file.FullName, [System.Text.Encoding]::UTF8)
            [System.IO.File]::WriteAllText($newFile, $content, [System.Text.Encoding]::Default)
        }
        "UTF8" {
            Write-Host "  编码: UTF-8 (无BOM) - 转换为ANSI" -ForegroundColor Yellow
            $content = [System.IO.File]::ReadAllText($file.FullName, [System.Text.Encoding]::UTF8)
            [System.IO.File]::WriteAllText($newFile, $content, [System.Text.Encoding]::Default)
        }
        "ANSI" {
            Write-Host "  编码: ANSI - 直接复制" -ForegroundColor Green
            Copy-Item -LiteralPath $file.FullName -Destination $newFile
        }
        "ASCII/ANSI" {
            Write-Host "  编码: ASCII/ANSI - 直接复制" -ForegroundColor Green
            Copy-Item -LiteralPath $file.FullName -Destination $newFile
        }
        default {
            Write-Host "  编码: $encoding - 无法处理,跳过" -ForegroundColor Red
        }
    }
}

Write-Host "`n处理完成!文件保存在: $newFolder" -ForegroundColor Magenta

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值