1、对vbs脚本入参的处理
For nLoop = 0 to WScript.Arguments.length -1
strArg = WScript.Arguments(nLoop)
....
NEXT nLoop
2、脚本中输出信息
WScript.Echo "Hello World!"
3、使用com组件
Set ComObject = nothing
Set ComObject = CreateObject(".....")
If IsNull(ComObject) = False Then
......
End If
切记在编写com组件作为输出参数时,必须要使用VARIANT *参数,不然会有麻烦的,呵呵。
4、访问数据库和CSV文件
Set conn =CreateObject("ADODB.Connection")
Set rs =CreateObject("ADODB.Recordset")
If UCase(strDBType) = "CSV" Then
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
sQuery = "Select test From "& strCSVFile
End If
If UCase(strDBType) = "ORACLE" Then
conn.Open "Provider=OraOLEDB.Oracle;User ID=" + _
StrDBUsr & ";Password=" & StrDBPWD & ";Data Source=" & strTNSName & ";PLSQLRSet = 1;"
sQuery = "select 'test' test from dual"
End If
RS.ActiveConnection = conn
RS.Source = sQuery
RS.Open()
If RS.EOF <> True Then
While RS.EOF = False
'获取值
strValue = RS.Fields("TERMINALNO")
RS.MoveNext()
Wend
End If
If RS.State <> adStateClosed Then
RS.Close()
End If
If Not RS Is Nothing Then Set RS = Nothing
If Not conn Is Nothing Then Set conn = Nothing
备注:
CSV文件只支持select,并不支持DML操作
5、VSB如何设置等待时间
WScript.Sleep millisecond
6、VSB如何操作XML
SET XMLDOC = CREATEOBJECT("MICROSOFT.XMLDOM")
XMLDOC.ASYNC = "FALSE"
IF STRFILENAME = "" THEN
STRFILENAME = "TEST.XML"
END IF
XMLDOC.LOAD(STRFILENAME)
STRQUERY = "/MAILCONFIG/ATTACHMENTS/ATTACHMENT"
SET COLITEM = XMLDOC.SELECTNODES(STRQUERY)
FOR EACH OBJITEM IN COLITEM
strFileName = OBJITEM.GETATTRIBUTE("FILENAME")
WSCRIPT.ECHO strFileName
NEXT
SET XMLDOC = CREATEOBJECT("MICROSOFT.XMLDOM")
XMLDOC.ASYNC = "FALSE"
IF STRFILENAME = "" THEN
STRFILENAME = "test.XML"
END IF
WSCRIPT.ECHO "UPDATE XML FILE : " & STRFILENAME
XMLDOC.LOAD(STRFILENAME)
STRQUERY = "/MAILCONFIG/VERSION"
SET COLITEM = XMLDOC.SELECTNODES(STRQUERY)
IF COLITEM.Length > 0 THEN
SET OBJITEM = COLITEM(0)
strMarjorver = OBJITEM.GETATTRIBUTE("MARJORVER")
strMinorrver = OBJITEM.GETATTRIBUTE("MINORRVER")
strRevnum = OBJITEM.GETATTRIBUTE("REVNUM")
strBuildNum = OBJITEM.GETATTRIBUTE("BUILDNUM")
strRevnumCur = YEAR(NOW) & MONTH(NOW) & DAY(NOW)
XMLMAINVER = "V"& strMarjorver &"." & strMinorrver &"." & strRevnumCur & "."
IF strRevnumCur = strRevnum THEN
strBuildNum = strBuildNum+1
ELSE
strBuildNum = 1
END IF
WSCRIPT.ECHO "SET LAST VERSION : " & XMLMAINVER & strBuildNum
OBJITEM.SETATTRIBUTE "REVNUM", strRevnumCur
OBJITEM.SETATTRIBUTE "BUILDNUM", strBuildNum
XMLDOC.SAVE strFileName
END IF
7、对文本文件的读取
Dim fso,OpenFile
Set fso = CreateObject("Scripting.FileSystemObject")
IF fso.FileExists(strFileName) THEN
Set OpenFile = fso.OpenTextFile(strFileName, 1)
Do While Not OpenFile.AtEndOfLine
strcontent = OpenFile.readline
Loop
OpenFile.CLOSE
ELSE
WSCRIPT.ECHO "Open File Error!"
END IF