Get encoding of a file in Windows

前端 未结 12 2229
忘了有多久
忘了有多久 2020-11-22 12:16

This isn\'t really a programming question, is there a command line or Windows tool (Windows 7) to get the current encoding of a text file? Sure I can write a little C# app b

12条回答
  •  天命终不由人
    2020-11-22 12:54

    Some C code here for reliable ascii, bom's, and utf8 detection: https://unicodebook.readthedocs.io/guess_encoding.html

    Only ASCII, UTF-8 and encodings using a BOM (UTF-7 with BOM, UTF-8 with BOM, UTF-16, and UTF-32) have reliable algorithms to get the encoding of a document. For all other encodings, you have to trust heuristics based on statistics.

    EDIT:

    A powershell version of a C# answer from: Effective way to find any file's Encoding. Only works with signatures (boms).

    # get-encoding.ps1
    param([Parameter(ValueFromPipeline=$True)] $filename)    
    begin {
      # set .net current directoy                                                                                                   
      [Environment]::CurrentDirectory = (pwd).path
    }
    process {
      $reader = [System.IO.StreamReader]::new($filename, 
        [System.Text.Encoding]::default,$true)
      $peek = $reader.Peek()
      $encoding = $reader.currentencoding
      $reader.close()
      [pscustomobject]@{Name=split-path $filename -leaf
                    BodyName=$encoding.BodyName
                    EncodingName=$encoding.EncodingName}
    }
    
    
    .\get-encoding chinese8.txt
    
    Name         BodyName EncodingName
    ----         -------- ------------
    chinese8.txt utf-8    Unicode (UTF-8)
    
    
    get-childitem -file | .\get-encoding
    

提交回复
热议问题