以前的代码,翻出来小改一下,凑合着能用- Dim fso, objFile, strExt, strFolder
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- For Each objFile in fso.GetFolder(".").Files
- strExt = fso.GetExtensionName(objFile.Path)
- strExt = LCase(strExt)
- If strExt = "txt" Or strExt = "html" Or strExt = "htm" Then
- strFolder = GetEncodingType(objFile.Path)
- If Not fso.FolderExists(strFolder) Then
- fso.CreateFolder(strFolder)
- End If
- objFile.Move strFolder & "\"
- End If
- Next
-
- MsgBox "Success!"
-
- Function GetEncodingType(file)
- Dim ado, bom, i, arr(), n
- Set ado = CreateObject("ADODB.Stream")
- ado.Mode = 3
- ado.Type = 1
- ado.Open
- ado.LoadFromFile file
- For i = 1 To 3
- bom = bom & Hex(AscB(ado.Read(1)))
- Next
- If InStr(bom, "FFFE") Or InStr(bom, "FEFF") Then
- GetEncodingType = "UNICODE"
- ElseIf bom = "EFBBBF" Then
- GetEncodingType = "UTF8"
- Else
- ado.Position = 0
- n = ado.Size - 1
- ReDim arr(n)
- For i = 0 to n
- arr(i) = ChrW(AscB(ado.Read(1)))
- Next
- If IsUtf8(Join(arr, "")) Then
- GetEncodingType = "UTF8NOBOM"
- Else
- GetEncodingType = "ANSI"
- End If
- End If
- ado.Close
- Set ado = Nothing
- End Function
-
- Function IsUtf8(ByRef str)
- Dim s, reg
- s = "[\xC0-\xDF]([^\x80-\xBF]|$)"
- s = s & "|[\xE0-\xEF].{0,1}([^\x80-\xBF]|$)"
- s = s & "|[\xF0-\xF7].{0,2}([^\x80-\xBF]|$)"
- s = s & "|[\xF8-\xFB].{0,3}([^\x80-\xBF]|$)"
- s = s & "|[\xFC-\xFD].{0,4}([^\x80-\xBF]|$)"
- s = s & "|[\xFE-\xFE].{0,5}([^\x80-\xBF]|$)"
- s = s & "|[\x00-\x7F][\x80-\xBF]"
- s = s & "|[\xC0-\xDF].[\x80-\xBF]"
- s = s & "|[\xE0-\xEF]..[\x80-\xBF]"
- s = s & "|[\xF0-\xF7]...[\x80-\xBF]"
- s = s & "|[\xF8-\xFB]....[\x80-\xBF]"
- s = s & "|[\xFC-\xFD].....[\x80-\xBF]"
- s = s & "|[\xFE-\xFE]......[\x80-\xBF]"
- s = s & "|^[\x80-\xBF]"
- Set reg = New RegExp
- reg.Pattern = s
- IsUtf8 = Not reg.Test(str)
- Set reg = Nothing
- End Function
复制代码
|