如何在VBscript中使用通配符查找和读取文件?

OmZ*_*OmZ 2 io vbscript file

我试图读取 XML 文件失败(每台计算机中的文件名都发生了变化)。如何使用通配符读取文件?
例如:D:\Logs\*.xml

脚本:

Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Logs\*.xml", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "server1 ", "server889 ")
Set objFile = objFSO.OpenTextFile("D:\Logs\*.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Closeenter code here
Run Code Online (Sandbox Code Playgroud)

Jos*_*efZ 5

没有外卡VBScript。对Method获得FilesFolder对象集合中的每个元素重复你的语句组GetFolder

Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("D:\Logs")

Set colFiles = objFolder.Files
For Each oFile in colFiles
    If UCase(objFSO.GetExtensionName(oFile.name)) = "XML" Then
        Set objFile = objFSO.OpenTextFile(oFile.Path, ForReading)
        strText = objFile.ReadAll
        objFile.Close
        If Instr( 1, strText, "server1 ", vbTextCompare) > 0 Then
            strNewText = Replace(strText, "server1 ", "server889 ", 1, -1, vbTextCompare)
            Set objFile = objFSO.OpenTextFile(oFile.Path, ForWriting)
            objFile.WriteLine strNewText
            objFile.Close
        End If
    End If
Next
Run Code Online (Sandbox Code Playgroud)

资源:FileSystemObject的