Today saw a conversion code format VBS script, but can only convert 2 kinds, modified a bit, you can specify the conversion format. Support:
- Utf-8
- ANSI or gb2312
- Unicode
Conversions between the other. Automatically detect the file format, manually specify the output format, the default output format is Utf-8. You can change the file encoding directly, or you can export it to a different file. Supports mandatory overwrite options.
To use:
cscript chcode.vbs inputfile.txt
inputfile.txt
converts to utf-8 format encoding
cscript chcode.vbs inputfile.txt outputfile.txt -o ansi
Will turn inputfile.txt
into a ansi
coded outputfil.txt, the original file unchanged.
The code is as follows:
' Change the code of a file.' by Eric Brown' 2015/3/20' Get the argumentsSetargv = WScript.ArgumentsifArgv. Length =0 Then PagerPrinthelpEnd if' The default codeIncode =""Outcode ="Utf-8"Haveout =FalseHaveincode =FalseGetInput =FalseInFile =""OutFile =""' If force, 'll replace the output' without asking.force =False' Analyse the arguments fori =0 toArgv. Length-1Isoption =False ifStrComp (argv (i),"-I.", vbTextCompare) =0 Theni = i +1Incode = argv (i) Haveincode =TrueIsoption =True ElseIfStrComp (argv (i),"-O", vbTextCompare) =0 Theni = i +1Outcode = argv (i) Isoption =True ElseIfStrComp (argv (i),"-F", vbTextCompare) =0 Thenforce =TrueIsoption =True ElseIfStrComp (argv (i),"-H", vbTextCompare) =0 Then PagerPrinthelpEnd if if notIsoption Then if notGetInput ThenInFile = argv (i) GetInput =True ElseOutFile = argv (i) Haveout =True End if End ifNextif notHaveout ThenOutFile = InFileEnd if' If output isn ' t specified' Make a temp fileif notHaveout ThenOutFile = OutFile &"~~~~~~~"End if' is the output format supported?ifStrComp (LCase (Outcode),"Utf-8") <>0 and_ StrComp (LCase (Outcode),"gb2312") <>0 and_ StrComp (LCase (Outcode),"Unicode") <>0 Then ifStrComp (LCase (Outcode),"ANSI") ThenOutcode ="gb2312" ElseWScript.Echo"Unsupported format:"& Outcode Wscript.QuitEnd ifEnd ififStrComp (LCase (Incode),"ANSI") =0 ThenIncode ="gb2312"End ifPagerCheckcode (InFile)' If The output is already existed' Check It unless-f is specifiedSetFSO = CreateObject ("Scripting.FileSystemObject")ifFso. FileExists (OutFile) and notForce ThenChoice = Msgbox (OutFile &"has been existed!"& VbCrlf & _"Does want to replace it?", vbquestion + vbYesNo, _"Output file has been existed")ifChoice = vbno ThenWscript.QuitEnd ifEnd ifSetInstream = CreateObject ("ADODB.stream")SetOutStream = 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. CloseSetInstream = 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. CloseSetOutStream = Nothing' If not specify the output file' then replace the input fileif notHaveout Then SetSrcfile = Fso.getfile (inFile) srcfile.deleteSetSrcfile = Fso.getfile (outFile) srcfile.name = InFileEnd ifFunctionCheckcode (sourcefile)' WScript.Echo ' Checking: "& SourceFile DimStreamSetStream = CreateObject ("ADODB.stream") stream. Type =1 ' adTypeBinaryStream. Mode =3 ' adModeReadWriteStream. Open Stream. Position =0Stream. LoadFromFile sourcefile Bin = Stream.read (2)ifAscB (MidB (Bin,1,1)) = &hef and_ AscB (MidB (Bin,2,1)) = &hbb ThenCodes ="Utf-8" ElseIfAscB (MidB (Bin,1,1)) = &hff and_ AscB (MidB (Bin,2,1)) = &hfe ThenCodes ="Unicode" ElseCodes ="gb2312" End if if notHaveincode ThenIncode = CodesEnd if ifStrComp (LCase (Incode), Codes) <>0 ThenWScript.Echo"Detected input format is:"& Codes &_ VbCrlf &"But you specified"& Incode &"."Wscript.QuitEnd ifStream. CloseSetstream = NothingEnd FunctionFunctionPRINTHELP () message ="Usage:cscript chcode.vbs infilename (outfilename)"& _"(Options)"& VbCrlf & _"If The Outfilename is not a specified, this program"& _"Would change the infilename ' s Code"& VbCrlf & _"Options"& VbCrlf & _"-I [Incode]: Specify the code"& _"of input file."& VbCrlf &_"If not specified, program would"& _"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"& VBCRLF WScript.Echo Message Wscript.QuitEnd Function
A VBS script that converts encoded formats