标题: [转贴] VBScript脚本操作数据库、XML文件等技巧 [打印本页]
作者: find 时间: 2012-3-3 21:22 标题: VBScript脚本操作数据库、XML文件等技巧
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
复制代码
http://dongbian.blog.51cto.com/2694115/702322
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |