How to bulk convert unicode file names to Ascii fi

2020-03-08 07:04发布

问题:

Problem: For some odd reason, the windows zip util will not zip up folders with Unicode file names. So, I need to convert a large set of filenames (not the contents) to ASCII files names. The answers here discuss content conversion

How do I mass/bulk convert/rename the file name itself in windows CMD line or Power Shell. I don't care about what the outputfile name has extra1 etc.

//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

回答1:

It took me a while, since I am not a powershell guy clearly... but it worked, and I am sharing!!

  1. Goto the Dir you want cd c:\MyDirectoryWithCrazyCharacterEncodingAndUnicode
  2. Fire this script away!

Copy and past the script in your Powershell windows

     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
    }

While searching I also found out how to fix the encoding for others, for people who keep getting output encoding to ASCII or Unicode all the time, you can set output encoding to whatever encoding you want from Microsoft blog $OutputEncoding

Issues 1, 2, 3 for bulk Hex to Ascii just replace the file names with variable you want to input