PowerShell 搜索文件编码格式

 1 $dirFind='D:\BeisenDev\Src\Web.CloudSetting'
 2 
 3 
 4 function Get-Encoding
 5 {
 6   param
 7   (
 8     [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
 9     [Alias('FullName')]
10     [string]
11     $Path
12   )
13 
14   process 
15   {
16     $bom = New-Object -TypeName System.Byte[](4)
17         
18     $file = New-Object System.IO.FileStream($Path, 'Open', 'Read')
19     
20     $null = $file.Read($bom,0,4)
21     $file.Close()
22     $file.Dispose()
23     
24     $enc = [Text.Encoding]::ASCII
25     if ($bom[0] -eq 0x2b -and $bom[1] -eq 0x2f -and $bom[2] -eq 0x76) 
26       { $enc =  [Text.Encoding]::UTF7 }
27     if ($bom[0] -eq 0xff -and $bom[1] -eq 0xfe) 
28       { $enc =  [Text.Encoding]::Unicode }
29     if ($bom[0] -eq 0xfe -and $bom[1] -eq 0xff) 
30       { $enc =  [Text.Encoding]::BigEndianUnicode }
31     if ($bom[0] -eq 0x00 -and $bom[1] -eq 0x00 -and $bom[2] -eq 0xfe -and $bom[3] -eq 0xff) 
32       { $enc =  [Text.Encoding]::UTF32}
33     if ($bom[0] -eq 0xef -and $bom[1] -eq 0xbb -and $bom[2] -eq 0xbf) 
34       { $enc =  [Text.Encoding]::UTF8}
35         
36     [PSCustomObject]@{
37       Encoding = $enc
38       Path = $Path
39     }
40   }
41 }
42 
43 
44 Get-ChildItem  $dirFind -Filter *.aspx -Recurse | Get-Encoding

 

posted @ 2020-11-24 11:16  特洛伊-Micro  阅读(519)  评论(0编辑  收藏  举报