0

我是一个 vbscript 新手,想搜索一些我通过 ftp 下载的文件。我运行了一个脚本来下载文件现在我不确定如何运行脚本来搜索所有文件。这就是我所拥有的,但我认为我应该使用 select case 语句。

抱歉,如果这段代码看起来很粗糙

Sub ReadDownloads()

Dim strInput As String
strInput = "file\path\somefile.ftp"

Dim dhigh, dclose, dlow

Close #1
Open strInput For Input As #1

  Input #1, strReadLine
  Input #1, strReadLine


While Not EOF(1)
    Input #1, strReadLine
    'Debug.Print strReadLine
    arrbreakdown = Split(strReadLine, " ")
    'Debug.Print arrbreakdown(1)
    If Left(arrbreakdown(1), 7) = "itemsearchingfor" Then
        If Mid(arrbreakdown(1), 8, 1) = "c" Then
            dclose = arrbreakdown(3)
        ElseIf Mid(arrbreakdown(1), 8, 1) = "h" Then
            dhigh = arrbreakdown(3)
        ElseIf Mid(arrbreakdown(1), 8, 1) = "l" Then
            dlow = arrbreakdown(3)
        End If
    End If


Wend
Close #1

Debug.Print dclose
Debug.Print dhigh
Debug.Print dlow

Range("D2").Value = dclose
Range("E2").Value = dhigh
4

1 回答 1

1

如果您要在文件中搜索文本。第一步是把文件变成一个字符串。

以下功能将为您执行此操作:

' -----------------------------------------
Private Function getFileAsString(p_strFilePath)
    Dim objFSO, objFile
    Dim strFileContents

    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.OpenTextFile(p_strFilePath, 1, false) 
    strFileContents = objFile.ReadAll 

    'clean up
    Set objFSO = Nothing
    Set objFile = Nothing

    getFileAsString = strFileContents
End Function    

下一步是检查字符串中是否存在文本,即文件内容。

下面的函数将为您执行此操作,它利用 VBInStr函数,该函数返回文本出现次数的整数。

' ----------------------------------------- 
Public Function isTextPresent(p_strFileContents)
    Dim intTemp, blnTextFound

    'using 'InStr' function to see if a string exists  
    intTemp = InStr (p_strFileContents, "WHATEVER TEXT YOU ARE SEARCHING FOR")

    If intTemp > 0 Then
        blnTextFound = True
    Else
        blnTextFound = False
    End If    

    isTextPresent = blnTextFound
End Function
于 2013-07-09T11:48:45.043 回答