检查文件是否存在然后执行代码

问题描述 投票:1回答:1

我想检查并查看文件(digicel_nongsm.xml)是否存在于C:\ DSUtility目录中,如果存在,则执行以下代码:

' ****************************************
' MAKE PRETTY XML
' ****************************************

Option Explicit

Const strInputFile = "C:\DSUtility\digicel_nongsm.xml"
'Const strOutputFile = "C:\DSUtility\digicel_nongsm_pp.xml"

' ****************************************

Dim objInputFile, objOutputFile, strXML
Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim objXML : Set objXML = WScript.CreateObject("Msxml2.DOMDocument")
Dim objXSL : Set objXSL = WScript.CreateObject("Msxml2.DOMDocument")

' ****************************************
' Put whitespace between tags. (Required for XSL transformation.)
' ****************************************

Dim strOutputFile : strOutputFile = "C:\DSUtility\" & WScript.Arguments(0)

Set objInputFile = objFSO.OpenTextFile(strInputFile,1,False,-2)
Set objOutputFile = objFSO.CreateTextFile(strOutputFile,True,False)
strXML = objInputFile.ReadAll
strXML = Replace(strXML,"><",">" & vbCrLf & "<")
objOutputFile.Write strXML
objInputFile.Close
objOutputFile.Close

' ****************************************
' Create an XSL stylesheet for transformation.
' ****************************************

Dim strStylesheet : strStylesheet = _
    "<xsl:stylesheet version=""1.0"" x`mlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _`
    "<xsl:output method=""xml"" indent=""yes""/>" & _
    "<xsl:template match=""/"">" & _
    "<xsl:copy-of select="".""/>" & _
    "</xsl:template>" & _
    "</xsl:stylesheet>"

' ****************************************
' Transform the XML.
' ****************************************

objXSL.loadXML strStylesheet
objXML.load strOutputFile
objXML.transformNode objXSL
objXML.save strOutputFile

WScript.Quit

任何人都可以帮我吗?如果文件不存在,则不应执行脚本,而是继续查找文件。

xml wsh
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.