读书巴士吧 关注:2,250贴子:25,565
  • 6回复贴,共1

最近用deepseek弄了个UTF-8转UTF-16 LE的小工具

只看楼主收藏回复

【支持老版本PowerShell,自动临时获取管理员权限执行PowerShell 脚本,检测TXT文件是否为UTF-16 LE防止重复转换变成乱码,支持复杂文件名和文件夹名【我正在用的那个软件不支持。。。文件名里带个❤️就直接卡死】,搭配PowerShell 脚本的批处理文件,把文件夹直接丢进批处理文件里就能把整个文件夹里的TXT文件全部转换成UTF-16 LE,验证确保转换前后内容语义一致防止转换后TXT文件内容缺失】


IP属地:广东1楼2025-01-27 20:19回复
    终极修复版 PowerShell 脚本 (Convert-Encoding.ps1)
    下面的是代码开头
    param(
    [Parameter(Mandatory=$true)]
    [string]$targetPath
    )
    function Convert-File {
    param($file)
    try {
    # 使用高级编码检测方法
    $stream = [System.IO.File]::OpenRead($file.FullName)
    $reader = New-Object System.IO.StreamReader($stream, $true) # 自动检测编码
    $content = $reader.ReadToEnd()
    $detectedEncoding = $reader.CurrentEncoding
    $reader.Close()
    $stream.Close()
    # 跳过已经是UTF-16 LE的文件
    if ($detectedEncoding.BodyName -eq 'utf-16') {
    Write-Host "[⚠️跳过] $($file.FullName) (已是UTF-16编码)" -ForegroundColor Cyan
    return
    }
    # 二进制模式验证原始编码
    $bytes = [System.IO.File]::ReadAllBytes($file.FullName)
    $validationContent = $detectedEncoding.GetString($bytes)
    if ($validationContent -ne $content) {
    throw "编码验证失败:检测到编码不一致"
    }
    # 写入UTF-16 LE带BOM
    [System.IO.File]::WriteAllText(
    $file.FullName,
    $content,
    [System.Text.Encoding]::Unicode
    )
    Write-Host "[✅成功] $($file.FullName) (原编码: $($detectedEncoding.EncodingName))" -ForegroundColor Green
    } catch {
    Write-Host "[❌失败] $($file.FullName)" -ForegroundColor Red
    Write-Host "错误详情: $($_.Exception.Message)" -ForegroundColor Yellow
    # 保留原始文件备份
    $backupPath = $file.FullName + ".bak"
    if (-not (Test-Path $backupPath)) {
    Copy-Item $file.FullName $backupPath
    }
    }
    }
    # 主程序
    if (Test-Path -LiteralPath $targetPath -PathType Container) {
    Get-ChildItem -LiteralPath $targetPath -Filter *.txt -Recurse -File | ForEach-Object {
    Convert-File $_
    }
    } else {
    Convert-File (Get-Item -LiteralPath $targetPath -Force)
    }
    代码在这就结束了
    配套批处理文件更新 (拖放转换.bat)
    下面的是代码开头
    @璐村惂鐢ㄦ埛_000076K馃惥 off
    chcp 65001 > nul
    setlocal disabledelayedexpansion
    if "%~1"=="" (
    echo 请将文件或文件夹拖放到此批处理文件图标上
    pause
    exit /b
    )
    set "psPath=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
    set "scriptPath=%~dp0Convert-Encoding.ps1"
    echo 正在处理:%~1
    echo 注意:转换失败的文件会自动生成.bak备份
    "%psPath%" -ExecutionPolicy Bypass -NoProfile -Command ". \"%scriptPath:\=\\%\" -targetPath \"%~f1\""
    pause
    代码在这就结束了
    使用说明
    保存文件
    将两个脚本保存到同一文件夹
    确保文件名精确匹配:
    拖放转换.bat
    Convert-Encoding.ps1
    执行转换
    直接拖放 文件或文件夹 到批处理文件图标上
    根据UAC提示授予管理员权限


    IP属地:广东2楼2025-01-27 20:21
    收起回复
      2025-08-15 21:37:37
      广告
      不感兴趣
      开通SVIP免广告
      大佬,你会不会修改apk安装包?


      IP属地:辽宁来自Android客户端4楼2025-01-28 08:14
      收起回复