Recently, we need to use SourceInsight to view and analyze project code developed under Linux system. We know that the default encoding format of text files in Linux system is UTF-8, while the default encoding format in Windows Chinese system is Gb2312. It's not bad to have different encoding formats in the system. The key is that SourceInsight does not support UTF-8, resulting in all Chinese annotations in the project code being garbled!
Since SourceInsight does not support UTF-8 encoded files, we have to find a way to convert UTF-8 code files into GB2312 encoded. The first thing I thought of was to search for "batch encoding conversion tools" online. After downloading, I found that the encoded and converted files always have garbled codes, which may be a bug in the tool. Since you cannot use tool conversion, you can write a program yourself for encoding conversion. However, if you think about it carefully, you will find that the C++/java encoding conversion program is rather verbose, and it can be easily done if you write a shell script under Linux. Fortunately, there are vbs scripts under Windows that can help us achieve this goal easily.
'-------------------------------------------------
'Function name:ReadFile
'Function: Use objects to read text files in various formats
'-------------------------------------------------
Function ReadFile(FileUrl, CharSet)
Dim Str
Set stm = CreateObject("")
= 2
= 3
= CharSet
FileUrl
Str =
Set stm = Nothing
ReadFile = Str
End Function
We use the ReadFile function to read a file, where FileUrl specifies the path to the file, CharSet specifies the original encoding format of the file, and use the object to read the file content to Str.
'-------------------------------------------------
'Function name: WriteToFile
'Function: Use objects to write text files in various formats
'-------------------------------------------------
Function WriteToFile (FileUrl, Str, CharSet)
Set stm = CreateObject("")
= 2
= 3
= CharSet
Str
FileUrl, 2
Set stm = Nothing
End Function
Then use WriteToFile to write Str back to the original file FileUrl and set the new encoding CharSet.
'-------------------------------------------------
'Function name:ConvertFile
'Function: encode and convert a file
'-------------------------------------------------
Function ConvertFile(FileUrl)
Call WriteToFile(FileUrl, ReadFile(FileUrl, SrcCode), DestCode)
End Function
In this way, ConvertFile encapsulates the above two functions and implements the encoding conversion of file FileUrl, where the original encoding SrcCode and the target encoding DestCode are global variables.
If we only encode and convert one file, we don't have to go through so much trouble. We hope to encode and convert all files in any file or folder to achieve the purpose of batch conversion.
'Function name:ConvertDir
'Function: Encoding and converting files in any directory
'-------------------------------------------------
Function ConvertDir(DirUrl)
If (DirUrl) Then
Call ConvertFile(DirUrl)
Else
Call SearchDir(DirUrl)
End If
End Function
The function ConvertDir encodes and converts files/folders of any path. Use the object's FileExists function to determine whether the path corresponds to a file or a folder. If it is a file, directly call ConvertFile for encoding and conversion, otherwise, call SearchDir to process the folder.
'-------------------------------------------------
'Function name: SearchDir
'Function: Recursively search the files in the directory and perform encoding conversion
'-------------------------------------------------
Function SearchDir(path)
Set folder = (path)
Set subfolders =
Set Files =
For Each i In Files
Call ConvertFile()
Next
For Each j In subfolders
Call SearchDir()
Next
End Function
The function SearchDir is recursive. First, call getfolder to create a folder object, and then remove the subfolders and subfile collection files in the folder. For each subfile, you can just call ConvertFile directly for encoding and conversion, while for each subfolder, you can call SearchDir to recursively process it.
'-------------------------------------------------
'Set encoding: default utf-8 --> gb2312
'-------------------------------------------------
SrcCode="utf-8"
DestCode="gb2312"
'-------------------------------------------------
'Resolve parameters
'-------------------------------------------------
Set fs = CreateObject("")
Set objArgs =
If >0 Then
For I = 0 To - 1
FileUrl = objArgs(I)
Call ConvertDir(FileUrl)
Next
Else
MsgBox "No file/folder was dragged in!"
End If
MsgBox "Conversion successful!"
Finally, by parsing the parameters of the script file, since each parameter corresponds to a file/folder path, pass it to ConvertDir. The default here is to convert UTF-8 encoding into GB2312 encoding, and readers can modify it themselves according to their own needs.
Save the above code as, just drag any number of files to the script file. Or use the command line.
> [filepath]
It should be noted that the file encoding is converted in-place, and it is best to back up the original file/folder before conversion.
Finally, all the code of the script file is attached.
'/*===========================================================
' * Intro Just drag multiple files/folders to convert to the file
' * FileName
' * Author Florian
' * Version v1.0
' * LastModify 2014-06-11 00:39:58
' *==========================================================*/
'-------------------------------------------------
'Set encoding: default utf-8 --> gb2312
'-------------------------------------------------
SrcCode="utf-8"
DestCode="gb2312"
'-------------------------------------------------
'Resolve parameters
'-------------------------------------------------
Set fs = CreateObject("")
Set objArgs =
If >0 Then
For I = 0 To - 1
FileUrl = objArgs(I)
Call ConvertDir(FileUrl)
Next
Else
MsgBox "No file/folder was dragged in!"
End If
MsgBox "Conversion was successful!"
'-------------------------------------------------
'Function name:ConvertDir
'Function: Encoding and converting files in any directory
'-------------------------------------------------
Function ConvertDir(DirUrl)
If (DirUrl) Then
Call ConvertFile(DirUrl)
Else
Call SearchDir(DirUrl)
End If
End Function
'-------------------------------------------------
'Function name: SearchDir
'Function: Recursively search the files in the directory and perform encoding conversion
'-------------------------------------------------
Function SearchDir(path)
Set folder = (path)
Set subfolders =
Set Files =
For Each i In Files
Call ConvertFile()
Next
For Each j In subfolders
Call SearchDir()
Next
End Function
'-------------------------------------------------
'Function name:ConvertFile
'Function: encode and convert a file
'-------------------------------------------------
Function ConvertFile(FileUrl)
Call WriteToFile(FileUrl, ReadFile(FileUrl, SrcCode), DestCode)
End Function
'-------------------------------------------------
'Function name:ReadFile
'Function: Use objects to read text files in various formats
'-------------------------------------------------
Function ReadFile(FileUrl, CharSet)
Dim Str
Set stm = CreateObject("")
= 2
= 3
= CharSet
FileUrl
Str =
Set stm = Nothing
ReadFile = Str
End Function
'-------------------------------------------------
'Function name: WriteToFile
'Function: Use objects to write text files in various formats
'-------------------------------------------------
Function WriteToFile (FileUrl, Str, CharSet)
Set stm = CreateObject("")
= 2
= 3
= CharSet
Str
FileUrl, 2
Set stm = Nothing
End FunctionView Code