I have this litle problem, which is propably very futile, but still I cant't find the solution.
I have this function which scans a directory for all kinds of files and returns all the filenames with the right extention. After that I cut of the directory string, put it into an array and I wish to pass it on to the next sub.

Public Function SearchList()
        Dim directorypath, FileName As String
        Dim Test, Test1, Testlength, FleNmLength As Integer
        Dim FileNames() As String

        Try

            Dim patterns() As String = {"*.pdf", "*.dwg", "*.dxf", "*.sat"}
            Dim foundFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            foundFiles = My.Computer.FileSystem.FindInFiles("C:\Users\Floris\Documents\school", "*", True, FileIO.SearchOption.SearchAllSubDirectories, patterns)

            Dim i As Integer
            i = foundFiles.Count
            ReDim FileNames(i)

            Dim file As String
            For Each file In foundFiles
                Testlength = file.Count
                Test = file.LastIndexOf("\")
                Test1 = Test + 1
                FleNmLength = Testlength - Test1
                FileName = file.Substring(Test1, FleNmLength)
                ' Debug.WriteLine(FileName)
                FileNames(i) = FileName
                ' Debug.WriteLine(FileNames(i))
            Next

            ' foundFiles = Nothing
            ArrCnt = i - 1

            Return FileNames

        Catch exception As System.UnauthorizedAccessException
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

       

    End Function

But when it arrives in the next sub, it does have all the indices, but every index is set to nothing. It doesn't contain any information anymore. This is something I just don't grab anymore.

Private Sub SearchDrwNosGA()
        Dim inc, maxrows As Integer

        'Dim RawData As String()
        Dim ds As New DataSet
        Dim Naam, GA As String
        Dim sql1 As String
        Dim con As New OleDb.OleDbConnection


        Dim obj As Minimal
        obj = New Minimal
        FileNames = SearchList()

        Naam = PrjNoStr

        If ds.Tables.Contains("Bacon Sandwich") Then
            ds.Tables("Bacon Sandwich").Rows.Clear()
        End If

        Dim da As OleDb.OleDbDataAdapter

        con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source =C:\Users\Floris\Documents\visuele troep\ENGDIF50\Exel test\TestMap.xlsx;Extended Properties=""Excel 12.0 xml;HDR=Yes;ReadOnly=0;"""
        con.Open()
        'OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Docs\\Book2.xlsx;Extended Properties='Excel 12.0 xml;HDR=YES;'");           
        'Microsoft.Jet.OLEDB.4.0

        sql1 = "SELECT [F1] FROM [GA$] WHERE [F4] ='" & Naam & "';"  ' apostrofjes aan het begin en eind van de variabele zijn zeer belangrijk, anders geen query

        Debug.WriteLine(sql1)
        da = New OleDb.OleDbDataAdapter(sql1, con)
        da.Fill(ds, "Bacon Sandwich")

        con.Close()

        maxrows = ds.Tables("Bacon Sandwich").Rows.Count
        inc = -1

        ' Try
        If maxrows = 0 Then
            MsgBox("Querrie 1 is leeg", MsgBoxStyle.Exclamation)
            Exit Sub
        Else
            For A = 0 To maxrows - 1
                inc = A '+ 1
                GA = ds.Tables("Bacon Sandwich").Rows(inc).Item(0)
                Debug.WriteLine(GA)
                'ListBox1.Items.Add(GA) ' fill into Listbox
                For Each item As String In FileNames
                    If item.Contains(GA) Then
                        ListBox1.Items.Add(item) 'aan var hangen, extentie afknippen, extentie testen en in de juiste tekstdoos kieperen
                    End If
                Next item
            Next A
        End If
        'Catch ex As Exception
        'MsgBox(ex.Message)
        'End Try
        sql1 = Nothing
        GA = Nothing

        Debug.WriteLine(FileNames(i))
    End Sub

Is there someone who can enlighten me on this?

Member Avatar for Unhnd_Exception

Your not incrementing your i variable. Looks like your setting the all file names in the for each loop to the last index. add i += 1 after setting the filename.

Dim i As Integer
ReDim FileNames(foundFiles.Count - 1)             
Dim file As String            

For Each file In foundFiles               

    Testlength = file.Count               
    Test = file.LastIndexOf("\")                
    Test1 = Test + 1                
    FleNmLength = Testlength - Test1               
    FileName = file.Substring(Test1, FleNmLength)                
                 
    FileNames(i) = FileName                
    i += 1
Next             
         
ArrCnt = i - 1

Ooops, I'm starting to feel realy stupid *blush*
I think this is the solution to my litle problem.

Thank you so much for helping me. :)

Eekhoorn, overlooking your original posted code and seeing this, seems that you are trying to get only the file name with extension from a file's full path.

For Each file In foundFiles
                Testlength = file.Count
                Test = file.LastIndexOf("\")
                Test1 = Test + 1
                FleNmLength = Testlength - Test1
                FileName = file.Substring(Test1, FleNmLength)
                ' Debug.WriteLine(FileName)
                FileNames(i) = FileName
                ' Debug.WriteLine(FileNames(i))
            Next

If that is the case, then you can simply use this to get only the filename with extension from a full file path.

Dim myCoolFile As String = "C:\Some Folder\Some SubFolder\Some FileName.someExtension"
        MsgBox(IO.Path.GetFileName(myCoolFile))

And this to get only the file name without the file extension.

Dim myCoolFile As String = "C:\Some Folder\Some SubFolder\Some FileName.someExtension"
        MsgBox(IO.Path.GetFileNameWithoutExtension(myCoolFile))

Even though thread is solved, I hope this helps. :)

Hey Codeorder,

Thats a nice litle trick. Thanks so much. It's very helpfull. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.