VBScript 如何读取任何以 testdata_.txt 开头的文件?

VBScript how to read any files that begins with testdata_.txt?

我有一个自动生成 testdata_1.txt、testdata_2.txt 等的系统。我想读取以 testdata_ 开头的文件并处理它,我该怎么做?我试过使用 testdata_* 但在下面的代码中它不起作用。任何帮助表示赞赏。非常感谢。

    sPath = "database/"
    sFile = "testdata_*.txt"
    sFileName = sPath & sFile    
    Set fso = Server.CreateObject("Scripting.FileSystemObject") 
    set fs = fso.OpenTextFile(Server.MapPath(sFileName), 1, true) 
    if not fs.AtEndOfStream then
        Do while not fs.AtEndOfStream 

遍历文件夹选择你想要的文件。我这里用的是Left()

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
    Set fldr = fso.GetFolder(Dirname)

    Set Fls = fldr.files
    On Error Resume Next

    For Each thing in Fls
         If Left(thing.name,9) = "TestData_" then 
              msgbox thing.name
         End If
    Next

这可以帮助你:

sPath = "database/"
sFile = "testdata_"

'Modify this line to indicate the disk drive where the files are located.
sDir = "C:\" & sPath

Set obj_FS = CreateObject("Scripting.FileSystemObject")

Set obj_FolderBase = obj_FS.GetFolder(sDir)

For Each obj_File In obj_FolderBase.Files    
    If Mid(obj_File.Name,1,9) = sFile then      
        set objReadFile = obj_FS.OpenTextFile (obj_File.Name, 1, False) 
        content = objReadFile.ReadAll 
        objReadFile.close 
        Wscript.Echo content           
    End If       
Next