【问题标题】:Check to see if file exists and then execute code检查文件是否存在然后执行代码
【发布时间】:2016-06-23 20:10:55
【问题描述】:

我想检查文件(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】:

问题

  • 如何在 wsh 中测试文件是否存在

解决方案

  • 使用ActiveXObject("Scripting.FileSystemObject")FileExists方法

示例

<?xml version="1.0"?>
<package>
<job id="default" filename="file_exists.wsf">
<script language="jscript">
<![CDATA[
    //  <beg-block>
    //  - caption: demo file existence check in wsh jscript
    //    rectype: rrmicro004
    //    dmid:    "uu446mussi0driv"
    //    date:    "2019-04-12"
    //    tags:    file,path,exists
    //    notes:  |
    //      * seealso ;; https://stackoverflow.com/questions/38000929/check-to-see-if-file-exists-and-then-execute-code
    //  <end-block>

    // regionbeg_:uu310host:       function.init//
    function ost_fileExists(sFileSpec){
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var vout;
        if (fso.FileExists(sFileSpec)){
            vout = true;
        }else{
            vout = false;
        }
        //
        return(vout);
    }
    // regionend_:uu310host//

    // regionbeg_::       vars.init//
    var str_currdir = new ActiveXObject("WScript.Shell").CurrentDirectory.replace(/\\/g, '/');
    str_currfile    = 'file_exists.wsf';
    var mytest;
    // regionend_://

    // regionbeg_::       vars.populate//
    str_fullpath    = [str_currdir,'/',str_currfile].join('');
    str_fakepath    = [str_currdir,'/','fake_noexisto.txt'].join('');
    // regionend_://

    // regionbeg_::       show result//
    mytest = ost_fileExists(str_fullpath);
    if(mytest){
        WScript.Echo(str_fullpath +' '+"Really exist!!!");
    }
    mytest = ost_fileExists(str_fakepath);
    if(!mytest){
        WScript.Echo(str_fakepath +' '+"Does not exist!!!");
    }
    // regionend_://
]]>
</script>
</job>
</package>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2022-11-25
    • 2018-06-21
    • 2013-05-19
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多