批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[转贴] VBScript代码把信息保存到XML文件

这个脚本演示了如何保存与使用的MSXML到一个XML文件的信息。示例XML文件用于在网站上的相册
This script demonstrates how to save information to a xml file with the use of MSXML. The example xml file is used for the photo album on the site.

  1. '*****************************************************************
  2. '** Script: CreateXML.vbs
  3. '** Version: 1.0
  4. '** Created: 01/12/2009 9:51PM
  5. '** Author: Adriaan Westra
  6. '** E-mail:
  7. '** Purpose / Comments:
  8. '** Create xml file for photo album
  9. '**
  10. '**
  11. '** Changelog :
  12. '** 12-01-2009 9:51 : Initial version
  13. '**
  14. '*****************************************************************
  15. On Error Resume next
  16. Dim Version : Version = "1.0" ' Script version
  17. Dim Author : Author = "A. Westra"
  18. Dim objXML 'XML Document object
  19. Dim root 'Root element of the xml document
  20. Dim newNode ' XML Node object
  21. Dim cNode ' XML (child) Node object
  22. Dim cNodeText ' XML Text Node object
  23. '*****************************************************************
  24. '** Make sure the script is started with cscript
  25. If InStr(wscript.FullName, "wscript.exe") > 0 Then
  26. MsgBox "Please run this script with cscript.exe." & Chr(13) & _
  27. "For example : cscript " & WScript.ScriptName & " /?", _
  28. vbExclamation, WScript.ScriptName
  29. WScript.Quit(1)
  30. End If
  31. '*****************************************************************
  32. '** Get commandline parameters
  33. Set Args = Wscript.Arguments
  34. If Args.Count = 0 Then
  35. strImageDir = InputBox("Please give the directory name " & _
  36. "to process : ",wscript.scriptname, strPath)
  37. Else
  38. If InStr(Args(0),"/?") > 0 Or InStr(UCase(Args(0)),"/H") > 0 _
  39. Or InStr(UCase(Args(0)),"/HELP") > 0 Then
  40. DisplayHelp
  41. Wscript.quit(0)
  42. Else
  43. strImageDir = Args(0)
  44. End if
  45. End if
  46. Set objXML = CreateObject("Msxml2.DOMDocument.6.0")
  47. objXML.setProperty "SelectionLanguage", "XPath"
  48. '*****************************************************************
  49. '** Determine if the file exists
  50. strXMLFile = strImageDir & "\album.xml"
  51. Set objFSO = CreateObject("Scripting.FileSystemObject")
  52. If objFSO.FileExists(strXMLFile) Then
  53. '*****************************************************************
  54. '** Read the XML File
  55. objXML.load(strXMLFile)
  56. Else
  57. '*****************************************************************
  58. '** Create the XML File
  59. objXML.loadXML("")
  60. End If
  61. '*****************************************************************
  62. '** Process directory
  63. Set objImgDir = objFSO.GetFolder(strImageDir)
  64. For each objFile in objImgDir.Files
  65. If IsJPG(objFile.Name) Then
  66. arrTemp = split(objFile.Name, ".")
  67. strNode = arrTemp(0)
  68. '*****************************************************************
  69. '** Determine if the node exists
  70. If Not XmlNodeExists(strChildNode, objXML) Then
  71. '*****************************************************************
  72. '** Get the root element of the xml document
  73. Set root = objXML.documentElement
  74. '*****************************************************************
  75. '** Create the new node
  76. Set newNode = objXML.createNode(1, strNode, "")
  77. root.appendChild newNode
  78. Set cNode = objXML.createNode(1, "alt", "")
  79. Set cNodeText = objXML.createNode(3, "", "")
  80. cNodeText.Text = strNode
  81. cNode.appendChild cNodeText
  82. newNode.appendChild cNode
  83. Set cNode = objXML.createNode(1, "Title", "")
  84. Set cNodeText = objXML.createNode(3, "", "")
  85. cNodeText.Text = strNode
  86. cNode.appendChild cNodeText
  87. newNode.appendChild cNode
  88. End If
  89. End If
  90. Next
  91. '*****************************************************************
  92. '** Save the xml file
  93. objXML.save(strXMLFile)
  94. '*****************************************************************
  95. '** End the script
  96. wscript.quit
  97. '*****************************************************************
  98. '** Function: XmlNodeExists
  99. '** Version: 1.0
  100. '** Created: 1/12/2009 12:14PM
  101. '** Author: Adriaan Westra
  102. '** E-mail:
  103. '**
  104. '** Purpose / Comments:
  105. '** Determines if a node exists in XML
  106. '**
  107. '** Arguments :
  108. '** strNode :Name of the XML node
  109. '** oXML :XMl DOM Object
  110. '**
  111. '** Changelog :
  112. '** 1/12/2009 12:16PM : Initial version
  113. '**
  114. '*****************************************************************
  115. Function XmlNodeExists( strNode, oXML )
  116. On Error Resume next
  117. Set oNode = oXML.selectSingleNode(strNode)
  118. strNodetype = oNode.nodetype
  119. If err.number = 0 Then
  120. XmlNodeExists = True
  121. Else
  122. XmlNodeExists = False
  123. End if
  124. End Function
  125. '*****************************************************************
  126. '** Sub: DisplayHelp
  127. '** Version: 1.0
  128. '** Created: 24-03-2003 8:22
  129. '** Author: Adriaan Westra
  130. '** E-mail:
  131. '**
  132. '** Purpose / Comments:
  133. '** Display help for script
  134. '**
  135. '** Arguments :
  136. '**
  137. '** Wijzigingslog :
  138. '** 24-03-2003 8:22 : Initial version
  139. '**
  140. '*****************************************************************
  141. Sub DisplayHelp()
  142. strComment = string(2,"*")
  143. strCmntLine = String(79, "*")
  144. wscript.echo strCmntline
  145. wscript.echo strComment
  146. wscript.echo strComment & " Online help for " & _
  147. Wscript.scriptname & " version : " & Version
  148. wscript.echo strComment
  149. wscript.echo strComment & " Usage : cscript " & _
  150. Wscript.scriptname & " directoryname"
  151. wscript.echo strComment
  152. wscript.echo strComment & " Purpose : Create XML file " & _
  153. "for all images in given directory."
  154. wscript.echo strComment
  155. wscript.echo strComment & " Author : " & Author
  156. wscript.echo strComment & " E-mail : " & Email
  157. wscript.echo strComment
  158. wscript.echo strCmntline
  159. End Sub
  160. '*****************************************************************
  161. '** Function: IsJPG
  162. '** Version: 1.0
  163. '** Created: 12/29/2008 11:01PM
  164. '** Author: Adriaan Westra
  165. '** E-mail:
  166. '**
  167. '** Purpose / Comments:
  168. '** Determine if file is jpg image
  169. '**
  170. '** Arguments :
  171. '** strFilename : name of the file to check
  172. '**
  173. '** Wijzigingslog :
  174. '** 12/29/2008 11:02PM : Initial version
  175. '**
  176. '*****************************************************************
  177. Function IsJPG(strFilename)
  178. Set objRegExp = New RegExp
  179. objRegExp.Pattern = "\w.jpg"
  180. objRegExp.IgnoreCase = True
  181. IsJPG = objRegExp.Test(strFileName)
  182. End Function
复制代码


转自:http://www.jb51.net/article/29243.htm

返回列表