打开每个文件夹/子文件夹,直到没有更多

问题描述 投票:-2回答:2

我有代码搜索excel文件的特定文件夹路径并拉回结果。我无法弄清楚的是,如何选择整个目录并打开/搜索它遇到的每个文件夹。最好的解决方案是打开文件夹的IF语句如果可用,但我很难过。

先感谢您。

如果我需要更具描述性,请告诉我!

    Try
        excelapp = New Application
        excelapp.Visible = False


        strPath = TextBox2.Text
        'strPath = "C:\Users\asside\Documents\Test Program"

        strSearch = TextBox1.Text
        'strSearch = "soup"

        If TextBox1.Text = "" Then
            Form3.ShowDialog()
            Exit Sub

        End If

        itms(0, 0) = "Workbook"
        itms(1, 0) = "Worksheet"
        itms(2, 0) = "Cell"
        itms(3, 0) = "Text in Cell"
        fso = CreateObject("Scripting.FileSystemObject")
        fld = fso.GetFolder(strPath)

        strFile = Dir(strPath & "\*.xls*")
        Do While strFile <> ""
            wbk = excelapp.Workbooks.Open(
              Filename:=strPath & "\" & strFile,
              UpdateLinks:=0,
              ReadOnly:=True,
              AddToMru:=False)

            For Each wks In wbk.Worksheets
                rFound = wks.UsedRange.Find(strSearch)
                If Not rFound Is Nothing Then
                    strFirstAddress = rFound.Address
                End If
                Do
                    If rFound Is Nothing Then
                        Exit Do
                    Else
                        itmcnt += 1
                        ReDim Preserve itms(3, itmcnt)
                        itms(0, itmcnt) = wbk.Name
                        itms(1, itmcnt) = wks.Name
                        itms(2, itmcnt) = rFound.Address
                        itms(3, itmcnt) = rFound.Value
                    End If
                    rFound = wks.Cells.FindNext(After:=rFound)
                Loop While strFirstAddress <> rFound.Address
            Next

            wbk.Close(False)
            strFile = Dir()
        Loop

    Catch ex As Exception
        MsgBox(ex.Message, vbExclamation, "")
    End Try

    wOut = Nothing
    wks = Nothing
    wbk = Nothing
    fld = Nothing
    fso = Nothing
    excelapp.Visible = False
    excelapp = Nothing

    Dim savefilePath As String

    savefilePath = Form5.TextBox1.Text

    If savefilePath = "" Then
        savefilePath = "Z:\Eric Application\SoupSearch\Program Files\OutputFolder\OutputSearch.CSV"
    End If"
vb.net search
2个回答
0
投票

使用System.IO.Directory.GetFiles(string, string, SearchOption)使用搜索选项:System.IO.SearchOption.AllDirectories

Dim paths = IO.Directory.GetFiles("path", "*.xls*", IO.SearchOption.AllDirectories)

在你的例子中

Try
    excelapp = New Application
    excelapp.Visible = False

    strPath = TextBox2.Text
    'strPath = "C:\Users\asside\Documents\Test Program"

    strSearch = TextBox1.Text
    'strSearch = "soup"

    If TextBox1.Text = "" Then
        Form3.ShowDialog()
        Exit Sub
    End If

    itms(0, 0) = "Workbook"
    itms(1, 0) = "Worksheet"
    itms(2, 0) = "Cell"
    itms(3, 0) = "Text in Cell"
    fso = CreateObject("Scripting.FileSystemObject")
    fld = fso.GetFolder(strPath)

    For Each strfile In IO.Directory.GetFiles(strPath, "*.xls*", IO.SearchOption.AllDirectories)
        wbk = excelapp.Workbooks.Open(
            Filename:=strfile,
            UpdateLinks:=0,
            ReadOnly:=True,
            AddToMru:=False)

        For Each wks In wbk.Worksheets
            rFound = wks.UsedRange.Find(strSearch)
            If Not rFound Is Nothing Then
                strFirstAddress = rFound.Address
            End If
            Do
                If rFound Is Nothing Then
                    Exit Do
                Else
                    itmcnt += 1
                    ReDim Preserve itms(3, itmcnt)
                    itms(0, itmcnt) = wbk.Name
                    itms(1, itmcnt) = wks.Name
                    itms(2, itmcnt) = rFound.Address
                    itms(3, itmcnt) = rFound.Value
                End If
                rFound = wks.Cells.FindNext(After:=rFound)
            Loop While strFirstAddress <> rFound.Address
        Next

        wbk.Close(False)
    Next

Catch ex As Exception
    MsgBox(ex.Message, vbExclamation, "")
End Try

0
投票

使用方法IO.Directory.GetFiles并将searchOption-argument设置为AllDirectories

For Each f As String In IO.Directory.GetFiles("C:\", "*.xls", IO.SearchOption.AllDirectories)
    ' Your method to pull back results
Next
© www.soinside.com 2019 - 2024. All rights reserved.