Board logo

标题: [转贴] VBScript脚本操作数据库、XML文件等技巧 [打印本页]

作者: find    时间: 2012-3-3 21:22     标题: VBScript脚本操作数据库、XML文件等技巧

1、对vbs脚本入参的处理

  1. For nLoop = 0 to WScript.Arguments.length -1
  2.     strArg = WScript.Arguments(nLoop)
  3.      ....
  4. NEXT nLoop
复制代码


2、脚本中输出信息

  1. WScript.Echo "Hello World!"
复制代码


3、使用com组件

  1. Set ComObject = nothing
  2. Set ComObject = CreateObject(".....")
  3. If IsNull(ComObject) = False  Then
  4.    ......
  5. End If
复制代码


切记在编写com组件作为输出参数时,必须要使用VARIANT *参数,不然会有麻烦的,呵呵。

4、访问数据库和CSV文件

  1. Set conn =CreateObject("ADODB.Connection")
  2. Set rs =CreateObject("ADODB.Recordset")
  3. If UCase(strDBType) = "CSV" Then
  4. conn.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
  5.     sQuery = "Select test From "& strCSVFile
  6. End If
  7. If UCase(strDBType) = "ORACLE" Then
  8. conn.Open  "Provider=OraOLEDB.Oracle;User ID=" + _
  9.   StrDBUsr & ";Password=" & StrDBPWD & ";Data Source=" & strTNSName & ";PLSQLRSet = 1;"
  10.     sQuery = "select 'test' test from dual"
  11. End If
  12. RS.ActiveConnection = conn
  13. RS.Source = sQuery
  14. RS.Open()
  15. If RS.EOF <> True Then
  16.          While RS.EOF = False
  17.           '获取值
  18.           strValue = RS.Fields("TERMINALNO")
  19.              RS.MoveNext()
  20.          Wend
  21. End If
  22. If RS.State <> adStateClosed Then
  23.         RS.Close()
  24. End If
  25. If Not RS Is Nothing Then Set RS = Nothing
  26. If Not conn Is Nothing Then Set conn = Nothing
复制代码


备注:
CSV文件只支持select,并不支持DML操作

5、VSB如何设置等待时间

  1. WScript.Sleep millisecond
复制代码


6、VSB如何操作XML

    *     读取

  1. SET XMLDOC = CREATEOBJECT("MICROSOFT.XMLDOM")
  2. XMLDOC.ASYNC = "FALSE"
  3. IF STRFILENAME = "" THEN
  4.     STRFILENAME = "TEST.XML"
  5. END IF
  6. XMLDOC.LOAD(STRFILENAME)
  7. STRQUERY = "/MAILCONFIG/ATTACHMENTS/ATTACHMENT"
  8. SET COLITEM = XMLDOC.SELECTNODES(STRQUERY)
  9. FOR EACH OBJITEM IN COLITEM
  10.     strFileName = OBJITEM.GETATTRIBUTE("FILENAME")
  11.     WSCRIPT.ECHO strFileName
  12. NEXT
复制代码


    *      修改和保存

  1. SET XMLDOC = CREATEOBJECT("MICROSOFT.XMLDOM")
  2. XMLDOC.ASYNC = "FALSE"
  3. IF STRFILENAME = "" THEN
  4.     STRFILENAME = "test.XML"
  5. END IF
  6. WSCRIPT.ECHO "UPDATE XML FILE  : " & STRFILENAME
  7. XMLDOC.LOAD(STRFILENAME)
  8. STRQUERY = "/MAILCONFIG/VERSION"
  9. SET COLITEM = XMLDOC.SELECTNODES(STRQUERY)
  10. IF COLITEM.Length > 0 THEN
  11.     SET OBJITEM = COLITEM(0)
  12.     strMarjorver = OBJITEM.GETATTRIBUTE("MARJORVER")
  13.     strMinorrver = OBJITEM.GETATTRIBUTE("MINORRVER")
  14.     strRevnum = OBJITEM.GETATTRIBUTE("REVNUM")
  15.     strBuildNum = OBJITEM.GETATTRIBUTE("BUILDNUM")
  16.     strRevnumCur =  YEAR(NOW) & MONTH(NOW)  &  DAY(NOW)
  17.     XMLMAINVER = "V"& strMarjorver &"." & strMinorrver &"." & strRevnumCur &  "."
  18.     IF strRevnumCur = strRevnum THEN
  19.         strBuildNum = strBuildNum+1
  20.     ELSE
  21.         strBuildNum = 1
  22.     END IF
  23.     WSCRIPT.ECHO "SET LAST VERSION : " & XMLMAINVER & strBuildNum
  24.     OBJITEM.SETATTRIBUTE "REVNUM", strRevnumCur
  25.     OBJITEM.SETATTRIBUTE "BUILDNUM", strBuildNum
  26.     XMLDOC.SAVE strFileName
  27. END IF
复制代码


7、对文本文件的读取

  1. Dim fso,OpenFile
  2. Set fso = CreateObject("Scripting.FileSystemObject")
  3. IF fso.FileExists(strFileName) THEN
  4.     Set OpenFile = fso.OpenTextFile(strFileName, 1)
  5.     Do  While   Not   OpenFile.AtEndOfLine
  6.         strcontent   =   OpenFile.readline
  7.      Loop
  8.     OpenFile.CLOSE
  9. ELSE
  10.     WSCRIPT.ECHO "Open File Error!"
  11. END IF
复制代码


http://dongbian.blog.51cto.com/2694115/702322




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2