【改】利用Emeditor轉換檔案的編碼格式
阿新 • • 發佈:2019-02-08
'/*========================================================================= ' * Intro 因為Emeditor識別文字編碼的能力比較強,所以用Emeditor開啟我們想轉換的檔案,它都可以正常開啟的,開啟後我們再改變這個檔案的編碼,然後儲存之 ' * FileName CodeChange.vbee ' * Author yongfa365 ' * Version v1.0 ' * WEB http://www.yongfa365.com ' * Email yongfa365[at]qq.com ' * FirstWrite http://www.yongfa365.com/Item/CodeChange.vbee.html ' * LastModify 2007-10-06 02:42:01 ' * 根據柳永法的博文所改,增加第一個功能! ' *==========================================================================*/Set mainMenu=CreatePopupMenu mainMenu.Add "功能:編碼轉換",0 mainMenu.Add "", 0, eeMenuSeparator mainMenu.Add "指定資料夾下所有檔案存為指定編碼檔案",1 mainMenu.Add "", 0, eeMenuSeparator mainMenu.Add "當前文件存為gb2312",2 mainMenu.Add "當前文件存為utf-8",3 mainMenu.Add "當前文件存為指定編碼檔案",4 mainMenu.Add "", 0, eeMenuSeparator mainMenu.Add"所有文件存為指定編碼檔案",5 mainMenu.Add "所有文件存為gb2312",6 mainMenu.Add "所有文件存為utf-8",7 mainMenu.Add "", 0, eeMenuSeparator mainMenu.Add "程式說明",100 witchItem=mainMenu.Track Select Case witchItem Case 1 sPath = prompt("請輸入要處理的檔案所在的資料夾", "") If FolderExits(sPath) = False Thenalert "輸入的資料夾不存在,退出!" Quit End If bSubFolder = confirm("包括子資料夾?") alert(sPath) sExt = prompt("只處理這些字尾的檔案:(為空表示處理所有檔案,各字尾以“|”隔開)", "html|htm|asp|php|php4") NewCode = prompt("文件編碼轉換為:(936-->gb2312,65001-->utf-8)", "65001") If IsNumeric(NewCode) Then AllFiles = FilesTree(sPath,bSubFolder) Set re = New RegExp If Strcomp(sExt,"")=0 Then re.Pattern = "([^|]+)" Else re.Pattern = "([^|]+/.(" & sExt & "))" End If re.IgnoreCase = True re.Global = True Set Matches = re.Execute(AllFiles) If Matches.Count = 0 Then alert("沒有滿足該副檔名的檔案") Else editor.NewFile For Each oMatch In Matches editor.OpenFile oMatch.SubMatches(0) SaveFile oMatch.SubMatches(0), NewCode Next Close End If Else alert "輸入有誤,必須輸入數字,退出" Quit End If Case 2 SaveFile document.FullName, 936 Case 3 SaveFile document.FullName, 65001 Case 4 NewCode = prompt("您要將當前文件編碼轉換為:(936-->gb2312,65001-->utf-8)", "936|65001") If IsNumeric(NewCode) Then SaveFile document.FullName, NewCode Else alert "輸入有誤,必須輸入數字,退出" Quit End If Case 5 NewCode = prompt("您要將所有文件編碼轉換為:(936-->gb2312,65001-->utf-8)", "936|65001") If IsNumeric(NewCode) Then AllDocNum = editor.Documents.Count Set NowFile = editor.ActiveDocument For i = 0 To AllDocNum editor.ExecuteCommandByID 5376 + i SaveFile document.FullName, NewCode Next NowFile.Activate() Else alert "輸入有誤,必須輸入數字,退出" Quit End If Case 6 AllDocNum = editor.Documents.Count Set NowFile = editor.ActiveDocument For i = 0 To AllDocNum editor.ExecuteCommandByID 5376 + i SaveFile document.FullName, 936 Next NowFile.Activate() Case 7 AllDocNum = editor.Documents.Count Set NowFile = editor.ActiveDocument For i = 0 To AllDocNum editor.ExecuteCommandByID 5376 + i SaveFile document.FullName, 65001 Next NowFile.Activate() Case 100 Msg= "柳永法制作,http://www.yongfa365.com" Msg=Msg & vbcrlf & "本巨集在製作過程中有一塊地方用了半天時間才發現問題:" Msg=Msg & vbcrlf & "如果您的文件是英文與數字組合沒有雙位元組文字," Msg=Msg & vbcrlf & "並且文件是沒有BOM的utf-8格式,那麼再次開啟這個文件時," Msg=Msg & vbcrlf & "EmEditor還是會把這個文件編碼認為是系統預設的文件," Msg=Msg & vbcrlf & "如果是簡體中文系統,顯示的還是936 gb2312編碼," Msg=Msg & vbcrlf & "但他確實是utf-8的,只是這兩種編碼都可以正常開啟這個檔案。" alert(Msg) End Select Sub SaveFile(FileName, CodePage) editor.ExecuteCommandByID 4105 'Save As vbCrLf document.Encoding = CodePage 'Encoding gb2312-->936 utf-8-->65001 ... If CodePage = 65001 Or CodePage = 65005 Or CodePage = 65006 Then document.UnicodeSignature = False 'BOM document.Save FileName End Sub Function FolderExits(Folder) Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FolderExists(Folder) Then FolderExits = True Else FolderExits = False End If End Function Function FilesTree(sPath,bSubFolder) '遍歷一個資料夾下的所有檔案 Set oFso = CreateObject("Scripting.FileSystemObject") Set oFolder = oFso.GetFolder(sPath) str="" Set oFiles = oFolder.Files For Each oFile In oFiles '獲取每一個檔案 str = str & "|" & oFile.Path '以 | 分隔每個檔案 Next If bSubFolder Then '是否進入子資料夾 Set oSubFolders = oFolder.SubFolders For Each oSubFolder In oSubFolders str = str & "|" & FilesTree(oSubFolder.Path,bSubFolder)'遞迴 Next End If Set oFolder = Nothing Set oSubFolders = Nothing Set oFso = Nothing FilesTree = str '返回包含檔名稱的字串 End Function