' ******************************************************************* ' effect: Transfer convert file encoding format ' parameter meaning: Incode for incoming file encoding Outcode converted file Encoding ' to determine if the file type is ANSI type, if not, provide a choice whether you need to update the file automatically ' ******************************************************** Function read (path) turns the array of byte () into a string string Dim ado, A (), I, n Set ado = CreateObject ("ADODB"). Stream ") ADO. Type = 1:ado. Open ADO. LoadFromFile path n = ADO. Size-1 ReDim A (n) for i = 0 to n a (i) = ChrW (AscB (ADO. Read (1))) Next read = Join (A, "") End functionfunction Is_valid_utf8 (ByRef input) ' ByRef to increase efficiency Dim s, re Set re = New Regexp 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] "re. Pattern = s Is_valid_utf8 = (not re. Test (input) End functionfunction Checkcode (sourcefile) ' WScript.Echo "Checking:" & SourceFile Dim Stream set Stream = CreateObject ("ADODB.stream") stream. Type = 1 ' adtypebinary stream. Mode = 3 ' adModeReadWrite stream. Open Stream. Position = 0 stream. LoadFromFile sourcefile Bin = Stream.read (2) s = Read (sourcefile) ' reads file if Is_valid_utf8 (s) =-1 then ' determine if Utf-8codes = ' utf -8 "MsgBox sourcefile&" file is "&Codes&" non-ANSI Please note modify "' &hef 239 &hbb 187 &hff 255 &HFE 254 Else If AscB (MidB (Bin, 1, 1)) = &hef and _ AscB (MidB (Bin, 2, 1)) = &hbb then Codes = "Utf-8" MsgBox S ourcefile& "File is" &Codes& "non-ANSI Please note modify" ElseIf AscB (MidB (BiN, 1, 1)) = &hff and _ AscB (MidB (Bin, 2, 1)) = &hfe then Codes = "Unicode" MsgBox sourcefile& "file is "&Codes&" non-ANSI Please note modify "ElseIf AscB (MidB (Bin, 1, 1)) = &hfe and _ AscB (MidB (Bin, 2, 1)) = &hff Thencod ES = "Unicode big endian" MsgBox sourcefile& "file is" &Codes& "non-ANSI Please note modify" Codes = "Unicode" elsecodes = "gb2312" End If stream. Close Set stream = Nothing Checkcode = codesend Function ' *********************************************************** ' Function: Transfer convert file encoding format ' parameter meaning: Incode for incoming file encoding Outcode converted file Encoding ' ********************************************** Function Transfer (infile,incode,outcode,outfile) Set instream = CreateObject ("ADODB.stream") Set OutStream = CreateObject ("ADODB.stream") ' Open input fileinstream. Type = 2 ' Adtypetextinstream.mode = 3 ' Admodereadwriteinstream.charset = InCodeinstream.Openinstream.LoadFromFile InFile ' Read input filecontent = instream. ReadText ' Close input fileinstream. CloseSet INstream = Nothing ' Open output fileoutstream. Type = 2 ' Adtypetextoutstream.mode = 3 ' Admodereadwriteoutstream.charset = Outcodeoutstream.open ' Write to output fileouts Tream. WRITETEXT Contentoutstream. SaveToFile outFile, 2 ' adsavecreateoverwriteoutstream.flush ' Close output fileoutstream. CloseSet outstream = nothingend Function ' ******************************************************************* ' Function: Getdirectory get current directory ' parameter meaning: ' *******************************************************************function Getdirectory () Dim wshshellset wshshell=createobject ("Wscript.Shell") Getdirectory = Wshshell.currentdirectoryset WshShell = Nothing End Function ' ******************************************************************* ' ************** ' Function: Rrubstr takes the substring after the sign string in the string istr; the position of the search from the trailing end of the string ' parameter meaning: ' * * * Function rsubstr (ISTR, sign) Dim fnum,substr fnum = InStrRev (istr,sign) + Len (sign)-1 substr = Right (Istr,len (ISTR)-fnum) rsubstr = Substrend Function ' ************************************************* ' function: ' parameter meaning: ' *******************************************************************function Main () ' Create new file Set Nfso = CreateObject ("Scripting.FileSystemObject") ' to traverse all files under a folder Set oFSO = CreateObject ("S Cripting. FileSystemObject ") fold = Getdirectory () &" \ "Set ofolder = Ofso.getfolder (fold) Dim infileisexist = 0isTra Nsfer = 0isCount = 0 Set ofiles = Ofolder.files ' processes each file for every ofile in ofiles inFile = Ofile.pathif RS Ubstr (InFile, ".") <> "vbs" THEN Iscount = Iscount +1incode = Checkcode (infile) Outcode = "gb2312" if Incode <> outcode then CH Oice = Msgbox (InFile & "is not ANSI, please note!" & VbCrlf & _ "Do you want to transfer it?", Vbquestion + vbYesNo, _ "Output file has been existed") if choice = vbyes Then Transfer infile,incode,outcode,infile ' MsgBox inFile & ' format conversion successful! "IsTrAnsfer = Istransfer +1end If isexist = isexist+1end if End If Next set nfso = Nothing Set ntf = Nothing Set ofolder = Nothing Sets Ofiles = Nothing MsgBox "Co-check file:" &isCount& ", found format not file:" &isExist& ", co-conversion Success file:" & Istransferend Function Main
New check whether SQL script conforms to ANSI encoding format