【问题标题】:How to bulk convert unicode file names to Ascii file names windows CMD or powershell如何将 unicode 文件名批量转换为 Ascii 文件名 windows CMD 或 powershell
【发布时间】:2026-01-16 19:50:01
【问题描述】:

问题:出于某种奇怪的原因,windows zip util 不会up 压缩具有 Unicode 文件名的文件夹。因此,我需要将大量文件名(而不是内容)转换为 ASCII 文件名。答案here讨论内容转换

如何在 windows CMD 行或 Power Shell 中批量/批量转换/重命名文件名本身。我不关心输出文件名有什么 extra1 等。

//While this changes the content inside the file. it does not rename my file name!

  COPY /Y UniHeader.txt Unicode_Output.txt
  CMD /U /C Type ANSI_Input.txt >> Unicode_Output.txt

【问题讨论】:

  • 您的最佳选择将是找到一个处理 Unicode 文件名的第三方压缩实用程序。编写代码来重命名文件,虽然不是太难,但仍然比它的价值更麻烦 - 特别是因为它会给你留下一个用处相对较少的存档,因为文件名与其内容无关。跨度>
  • 好的,谢谢,我正在尝试 7zip 以取得更好的运气,但它也会崩溃。文件的名称无关紧要,因为无论如何它们都将作为 guid 提供。
  • Unicode - Ascii 转换是一项非常重要的任务。您可能对removing the diacritics 有一些运气,但这个错误很容易发生。士气:永远不要在文件名中使用非 ASCII 字符。 (我的母语有变音符号,所以我从 80 年代开始就一直在处理这样的问题......)
  • @vonPryz 这很有趣 :) 但我终于解决了,耶!

标签: windows powershell unicode command-line


【解决方案1】:

我花了一段时间,因为我显然不是一个 powershell 人......但它有效,I am sharing!!

  1. 转到您想要的目录cd c:\MyDirectoryWithCrazyCharacterEncodingAndUnicode
  2. 解雇这个脚本!

在您的 Powershell 窗口中复制并粘贴脚本

     foreach($FileNameInUnicodeOrWhatever in get-childitem)
     {
        $FileName = $FileNameInUnicodeOrWhatever.Name    
        $TempFile = "$($FileNameInUnicodeOrWhatever.Name).ASCII"    
        get-content $FileNameInUnicodeOrWhatever | out-file $TempFile -Encoding ASCII     
        remove-item $FileNameInUnicodeOrWhatever    
        rename-item $TempFile $FileNameInUnicodeOrWhatever
        # only if you want to debug
        # write-output $FileNameInUnicodeOrWhatever "converted to ASCII ->" $TempFile
    }

在搜索时,我还发现了如何为其他人修复编码,对于不断将输出编码为 ASCII 或 Unicode all the time, 的人,您可以设置输出 encoding to whatever encoding you want from Microsoft blog $OutputEncoding

问题123 for bulk Hex to Ascii just replace the file names with variable you want to input

【讨论】:

  • 这是不正确的,它只修改了文件内容,而不是文件名。
  • 刚刚在示例文件夹上尝试过,但在包含尾随单引号 (') 的文件夹名称上出现“找不到路径”错误。
【解决方案2】:
$nonascii = [regex] "[^\x00-\x7F]"

Get-ChildItem -Attributes !Directory+!System | Rename-Item -NewName {
  '{0}{1}' -f ($_.BaseName -replace $nonascii, ''), $_.Extension
}

这会从目录中的所有文件名中删除所有非 ascii 字符。

如果您不想丢失任何字符,可以使用额外的替换语句添加音译映射:

$nonascii = [regex] "[^\x00-\x7F]"

Get-ChildItem -Attributes !Directory+!System | Rename-Item -NewName {
  '{0}{1}' -f ($_.BaseName -replace 'à', 'a' -replace 'é', 'e' -replace $nonascii, ''), $_.Extension
}

【讨论】: