VBS script implements batch conversion file encoding _vbs

Source: Internet
Author: User
Tags flush readfile

Recently, you need to use Sourceinsight to view the project code developed under Linux, and we know that the default encoding format for text files in Linux systems is UTF-8, and the default encoding format for Windows Chinese is Gb2312. The coding format in the system is different, the key is sourceinsight unexpectedly do not support UTF-8, resulting in the project code in the Chinese comments all garbled!

Since Sourceinsight does not support UTF-8 encoded files, we have to find a way to convert UTF-8 code files to GB2312 encoded. The first thought is to search the Internet "Batch code conversion tool", the results of the download after the conversion of the file will always appear garbled, may be a tool bug it. Since you can't use tool conversion, write a program to encode the conversion, but think about the C++/java code conversion program is more verbose, if you write a shell script under Linux can easily handle. Fortunately, there is a VBS script under windows that can help us easily achieve this goal.

Copy Code code as follows:

'-------------------------------------------------

' Function name: ReadFile

' Function: Use the ADODB.stream object to read text files in various formats

'-------------------------------------------------

Function ReadFile (FILEURL, CharSet)

Dim STR

Set stm = CreateObject ("ADODB.stream")

Stm. Type = 2

Stm.mode = 3

Stm.charset = CharSet

Stm. Open

Stm.loadfromfile FILEURL

STR = Stm.readtext

Stm. Close

Set stm = Nothing

ReadFile = Str

End Function

We use the ReadFile function to read the file, where FileURL the path of the specified file, charset the original encoding of the specified file, and use the ADODB.stream object to read the file contents to Str.

Copy Code code as follows:

'-------------------------------------------------

' Function name: WriteToFile

' Function: Use ADODB.stream object to write text files in various formats

'-------------------------------------------------

Function WriteToFile (FILEURL, STR, CharSet)

Set stm = CreateObject ("ADODB.stream")

Stm. Type = 2

Stm.mode = 3

Stm.charset = CharSet

Stm. Open

Stm. WRITETEXT STR

Stm. SaveToFile FILEURL, 2

Stm.flush

Stm. Close

Set stm = Nothing

End Function

Then use WriteToFile to write Str back to the original file FileURL and set the new encoding charset.

Copy Code code as follows:

'-------------------------------------------------

' Function name: convertfile

' Function: Encode a file for conversion

'-------------------------------------------------

Function Convertfile (FILEURL)

Call WriteToFile (FILEURL, ReadFile (FILEURL, Srccode), Destcode)

End Function

In this way, the convertfile of the above two functions is encapsulated to realize the encoding conversion of the file FileURL, in which the original encoding Srccode and the destination encoding Destcode as global variables.

We don't have to go through all this trouble if we encode only one file. We want to be able to encode and convert all the files in any file or folder to the purpose of batch conversion.

Copy Code code as follows:
'-------------------------------------------------

' Function name: Convertdir

' Function: Encode and convert files in any directory

'-------------------------------------------------

Function Convertdir (Dirurl)

If FS. FileExists (Dirurl) Then

Call Convertfile (Dirurl)

Else

Call Searchdir (Dirurl)

End If

End Function

function Convertdir the file/folder of any path to encode the conversion, Use the FileExists function of the Scripting.FileSystemObject object to determine whether the path corresponds to a file or folder, and if it is a file, call Convertfile to encode the conversion directly, otherwise the Searchdir processing folder is invoked.

Copy Code code as follows:

'-------------------------------------------------

' Function name: Searchdir

' Function: Recursively look up files in a directory for coded conversions

'-------------------------------------------------

Function Searchdir (PATH)

Set folder = Fs.getfolder (path)

Set subfolders = Folder.subfolders

Set Files = folder. Files

For all I in Files

Call Convertfile (I.path)

Next

For all J in subfolders

Call Searchdir (J.path)

Next

End Function

The function Searchdir is recursive, first calling GetFolder to create the folder object, and then removing the subfolder collection subfolders and the file collection files within the folder. For each child, the Convertfile is invoked directly for encoding, whereas for each subfolder, the recursive call Searchdir is repeated.

Copy Code code as follows:

'-------------------------------------------------

' Set encoding: Default Utf-8--> gb2312

'-------------------------------------------------

Srccode= "Utf-8"

Destcode= "gb2312"

'-------------------------------------------------

' Parse parameters

'-------------------------------------------------

Set fs = CreateObject ("Scripting.FileSystemObject")

Set Objargs = wscript.arguments

If objargs.count>0 Then

For I = 0 to Objargs.count-1

FILEURL = Objargs (I)

Call Convertdir (FILEURL)

Next

Else

MsgBox "No files/folders dragged!" "

Wscript.Quit

End If

MsgBox "Conversion Successful! "

Finally, by parsing the parameters of the script file, because each parameter corresponds to a file/folder path, it is passed to the Convertdir. The default here is to convert UTF-8 encoding to GB2312 encoding, and readers can modify them according to their own needs.

Save the above code as Convertcode.vbs and just drag any number of files to the script file. or use the command line.

Copy Code code as follows:

> convertcode.vbs [filepath]

Note that the file encoding is converted in place and it is best to back up the original file/folder before converting.

Finally, attach all the code for the script file.

Copy Code code as follows:

'/*===========================================================
' * Intro drag multiple files/folders to be converted to this file
' * FileName convertcode.vbs
' * Author Florian
' * Version v1.0
' * lastmodify 2014-06-11 00:39:58
' *==========================================================*/

'-------------------------------------------------
' Set encoding: Default Utf-8--> gb2312
'-------------------------------------------------

Srccode= "Utf-8"
Destcode= "gb2312"

'-------------------------------------------------
' Parse parameters
'-------------------------------------------------

Set fs = CreateObject ("Scripting.FileSystemObject")
Set Objargs = wscript.arguments
If objargs.count>0 Then
For I = 0 to Objargs.count-1
FILEURL = Objargs (I)
Call Convertdir (FILEURL)
Next
Else
MsgBox "No files/folders dragged!" "
Wscript.Quit
End If
MsgBox "Conversion Successful! "

'-------------------------------------------------
' Function name: Convertdir
' Function: Encode and convert files in any directory
'-------------------------------------------------

Function Convertdir (Dirurl)
If FS. FileExists (Dirurl) Then
Call Convertfile (Dirurl)
Else
Call Searchdir (Dirurl)
End If
End Function


'-------------------------------------------------
' Function name: Searchdir
' Function: Recursively look up files in a directory for coded conversions
'-------------------------------------------------

Function Searchdir (PATH)
Set folder = Fs.getfolder (path)
Set subfolders = Folder.subfolders
Set Files = folder. Files
For all I in Files
Call Convertfile (I.path)
Next
For all J in subfolders
Call Searchdir (J.path)
Next
End Function

'-------------------------------------------------
' Function name: convertfile
' Function: Encode a file for conversion
'-------------------------------------------------

Function Convertfile (FILEURL)
Call WriteToFile (FILEURL, ReadFile (FILEURL, Srccode), Destcode)
End Function

'-------------------------------------------------
' Function name: ReadFile
' Function: Use the ADODB.stream object to read text files in various formats
'-------------------------------------------------

Function ReadFile (FILEURL, CharSet)
Dim STR
Set stm = CreateObject ("ADODB.stream")
Stm. Type = 2
Stm.mode = 3
Stm.charset = CharSet
Stm. Open
Stm.loadfromfile FILEURL
STR = Stm.readtext
Stm. Close
Set stm = Nothing
ReadFile = Str
End Function

'-------------------------------------------------
' Function name: WriteToFile
' Function: Use ADODB.stream object to write text files in various formats
'-------------------------------------------------

Function WriteToFile (FILEURL, STR, CharSet)
Set stm = CreateObject ("ADODB.stream")
Stm. Type = 2
Stm.mode = 3
Stm.charset = CharSet
Stm. Open
Stm. WRITETEXT STR
Stm. SaveToFile FILEURL, 2
Stm.flush
Stm. Close
Set stm = Nothing
End Functionview Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.