File 如何使用带VBscript的通配符查找和读取文件?
我尝试读取XML文件失败(每台计算机中的文件名都在更改)。 如何使用通配符读取文件?File 如何使用带VBscript的通配符查找和读取文件?,file,vbscript,io,File,Vbscript,Io,我尝试读取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
例如:
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
VBScript
中没有通配符。对通过GetFolder
方法获得的Folder
对象的Files
集合中的每个元素重复您的语句组:
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
资源:文件名是真正的*.xml还是仅仅是一个占位符?运行代码后会发生什么情况?没有或出现错误?否不是文件的真实名称,但由于文件名在每台计算机中都发生了更改,因此我必须使用通配符,如:“D:\Logs*.xml”。我得到了下一个错误:“错误的文件名或编号”@MrMush
*
(星号)是保留字符,请参阅不完全正确。某些FileSystemObject
方法在特定情况下支持通配符(例如:)。但是,打开文件(或迭代文件集合)时不支持通配符。