如何批量转换unicode文件名到Ascii文件名windows CMD或powershell

时间:2017-01-27 05:21:10

标签: windows powershell unicode command-line

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

Cannot compress files with UNICODE names

如何在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

2 个答案:

答案 0 :(得分:1)

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

  1. 转到你想要的目录cd c:\MyDirectoryWithCrazyCharacterEncodingAndUnicode
  2. 解开这个脚本吧!
  3. 复制并通过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

答案 1 :(得分:0)

$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
}