excelvb.netfindinfiles

Is the method filesystem.findinfiles available to ".xlsx" file ?


I am intending to write a sub to loop through a directory to find is there any file (xls, xlsx, txt, etc.) which contains the input sting。

I used filesystem.findinfiles method, but it seems that xlsx files couldn't be read with this method. ( I have done some tests, a xlsx file which contains the string could not be listed in the result)

I am thankful to any ideas or suggestions~

below is my code :

Dim Flist As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

Flist = My.Computer.FileSystem.FindInFiles("C:/some directory/", "inputstring", True,FileIO.SearchOption.SearchTopLevelOnly)

For Each Names In Flist
     foundlist.Add(Names)
next

Solution

  • You need .GetFiles. .FindInFiles is looking in the file itself, not the name of the file.

    Flist = My.Computer.FileSystem.GetFiles("C:\Users\xxx\Documents\Excel", FileIO.SearchOption.SearchTopLevelOnly, "*.xlsx")
    

    Notice that the parameters are in a different order and the Boolean value is not required.