0

I'm new to VB and I'm hoping someone can help with the first major problem I've encountered.

I've created a form, which:

  1. Allows me to specify a SearchString in a text box
  2. To specify a FolderPath using FolderBrowserDialog in a text box
  3. Pass the values in the text boxes as variables
  4. Return all files in the FolderPath, containing the SearchString, with wildcards, to a ListBox when a button is clicked.

The code behind the button is as follows:

Private Sub ListButton_Click(sender As Object, e As EventArgs) Handles ListButton.Click
    Dim fls
    Dim FolderPath As String
    Dim SearchString As String

    FolderPath = FolderPathBox.Text
    SearchString = SearchStringBox.Text

    fls = My.Computer.FileSystem.GetFiles(FolderPath,"*" & SearchString & "*")
    For Each f As String In fls
        MatchingFilesBox.Items.Add(f)
    Next
End Sub

However, after populating the SearchString and FolderPath text boxes with the following values respectively:

(1)
C:\Backup\Files

and clicking the button, the following error is returned:

Additional information: Conversion from string "* (1)*" to type 'Integer' is not valid.

The same error is displayed even if I do not specify a number e.g. "an" and I've not specifically configured any text boxes or classes or variables as data type integer.

I've simplified the code by removing the variables and wildcards from the equation and hard coding the path and a file name:

'fls = My.Computer.FileSystem.GetFiles(FolderPath,"*" & SearchString & "*")
fls = My.Computer.FileSystem.GetFiles("C:\Backup\Files", "abandoning.docx")

But the same error on converting to data type integer is displayed:

Additional information: Conversion from string "abandoning.docx" to type 'Integer' is not valid.

I'm flummoxed as to why or how an integer is being passed or retrieved in the file path. I've searched for answers to the error, but the articles I've read relate to number values, while mine doesn't; or to empty textboxes, which I believe I've eliminated; or use Replace, which I'm not.

Can anyone offer any guidance on overcoming this issue, so I can return all files in a folder containing a specific string in the filename?

2
  • Please read How to Ask and take the tour. After that, examine the line of code it fails on; Intellisense will aide you. GetFiles() does not have an overload that takes 2 strings. Commented Sep 14, 2017 at 17:52
  • I reviewed multiple example code, none of which oddly specified the SearchType. I've now read the How to Ask guide and taken the tour and you're right, Intellisense actually gave me the answer, so that's very helpful advice. Thank you, Plutonix. Commented Sep 15, 2017 at 12:46

1 Answer 1

1

You are passing in the incorrect number of parameters. The second parameter is supposed to be an enumeration of which is always a number. The last parameter is your wild card which can be a string. Try this:

fls = My.Computer.FileSystem.GetFiles("C:\Backup\Files", SearchOption.SearchTopLevelOnly "*.docx")

Look here for the reference as to what you are to pass in to the GetFiles() function: https://msdn.microsoft.com/en-us/library/t71ykwhb(v=vs.90).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

SearchOption.SearchTopLevelOnly was precisely what I was missing. Thank you Manuel!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.