一个转换编码格式的VBS脚本

今天看到了一个转换编码格式的VBS脚本程序,但是只能转换2种,改造了一下,可以自己指定转换格式。支持:

之间的相互转换。自动探测文件格式,手工指定输出格式,默认输出格式为utf-8。可以直接改变文件编码,,也可以输出为另外的一个文件。支持强制覆盖选项。

用法为:

cscript chcode.vbs inputfile.txt

会把inputfile.txt转换为utf-8格式编码

cscript chcode.vbs inputfile.txt outputfile.txt -o ansi

会把inputfile.txt转成为ansi编码的outputfil.txt,原文件不变。

代码如下:

argv = WScript.ArgumentsPrintHelpinCode = “”outCode = “utf-8″haveOut = FalsehaveInCode = FalsegetInput = FalseinFile = “”outFile = force = i = 0 to argv.Length – 1isOption = i = i + 1inCode = argv(i)haveInCode = TrueisOption = i = i + 1outCode = argv(i)isOption = force = TrueisOption = PrintHelpisOption getInput TheninFile = argv(i)getInput = TrueelseoutFile = argv(i)haveOut = haveOut thenoutFile = inFilehaveOut thenoutFile = outFile & StrComp(LCase(outCode), “utf-8”) <> 0 and _ StrComp(LCase(outCode), “gb2312”) <> 0 and _ StrComp(LCase(outCode), StrComp(LCase(outCode), “ansi”) thenoutCode = “gb2312″elseWScript.echo “Unsupported format: ” & outCodeWScript.QuitStrComp(LCase(inCode), “ansi”) = 0 theninCode = CheckCode(inFile)fso = CreateObject(“Scripting.FileSystemObject”)if fso.FileExists(outFile) and not force thenchoice = Msgbox(outFile & ” has been existed!” & vbCrlf & _” Do you want to replace it?”, vbQuestion + vbYesNo, _”Output file has been existed”)if choice = vbNo ThenWScript.Quitinstream = 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 fileoutstream.WriteText contentoutstream.SaveToFile outFile, 2 ‘adSaveCreateOverWriteoutstream.flush’Close output fileoutstream.ClosehaveOut thenset srcFile = fso.getFile(inFile)srcFile.deleteset srcFile = fso.getFile(outFile)srcFile.name = inFileCheckCode(Sourcefile)’WScript.echo “Checking: ” & SourcefileDim streamset stream = CreateObject(“Adodb.Stream”)stream.Type = 1 ‘adTypeBinarystream.Mode = 3 ‘adModeReadWritestream.Openstream.Position = 0stream.LoadFromFile SourcefileBin = stream.read(2)if AscB(MidB(Bin, 1, 1)) = &HEF and _AscB(MidB(Bin, 2, 1)) = &HBB ThenCodes = “utf-8″elseif AscB(MidB(Bin, 1, 1)) = &HFF and _AscB(MidB(Bin, 2, 1)) = &HFE ThenCodes = “unicode”elseCodes = haveInCode TheninCode = CodesStrComp(LCase(inCode), Codes) <> 0 thenWScript.echo “Detected input format is: ” & Codes &_vbCrlf & “But you specified ” & inCode & “.”WScript.Quitend ifstream.ClosePrintHelp()message = “Usage: cscript chcode.vbs inFileName (outFileName) ” & _”(Options)” & vbCrlf & _”If the outFileName is not specified, this program ” & _”will change the inFileName’s code” & vbCrlf & _”OPTIONS” & vbCrlf & _” -i [inCode]: Specify the code ” & _”of input file.”& vbCrlf &_”If not specified, program will ” & _”auto detect the code of the input file.” & vbCrlf & _” -o [outCode]: Specify the code of output file.” & _vbCrlf & “The default is utf-8″ & _vbCrlf & _” -f: If the output file is specified, don’t ask.” & _vbCrlf & “SUPPORTED FORMAT” & vbCrlf & _”utf-8″ & vbCrlf & _”ansi or gb2312″ & vbCrlf & _”unicode” & vbCrlfWScript.echo messageWScript.Quitend Function

片的时光如浮云般流过,我们的青春单薄的穿梭在蓝天之上。

一个转换编码格式的VBS脚本

相关文章:

你感兴趣的文章:

标签云: