简体   繁体   中英

How can I check if filename contains a portion of a string in vb.net

I have a userform in 2008 vb express edition. A part number is created from user input via a concat string. I want to then check if a certain portion of the part number exists in the existing file names in a directory. Below is a more detailed explanation.

This is my code for creating a part number from the user input on the form.

L_PartNo.Text = String.Concat(CB_Type.Text, CB_Face.Text, "(", T_Width.Text, "x", T_Height.Text, ")", mount, T_Qty.Text, weep, serv)

I then have the following code to tell the user if the configuration (part no) they just created exists

L_Found.Visible = True
If File.Exists("Z:\Cut Sheets\TCS Products\BLANK OUT SIGN\" & (L_PartNo.Text) & ".pdf") Then
        L_Found.Text = "This configuration exists"
      Else
        L_Found.Text = "This configuration does NOT exist"
      End If

This is where I need help. The part no will look like this BX002(30x30)A1SS I want to compare 002(30x30) (just this part of the file name) to all the files in one directory. I want a yes or no answer to the existance and not a list of all matching files. The code below is everything I've tried, not all at the same time.

Dim b As Boolean
b = L_PartNo.Text.Contains(NewFace)

Dim NewFace As String = String.Concat(CB_Face.Text, "(", T_Width.Text, "x", T_Height.Text, ")")
Dim NewFace = L_PartNo.Text.Substring(2, 10)

If filename.Contains(NewFace) Then
        lNewFace.Visible = False
      Else
        lNewFace.Visible = True
      End If

The code below was a translation from the answer in C# but it does not work either

Dim contains As Boolean = Directory.EnumerateFiles(path).Any(Function(f) [String].Equals(f, "myfilethree", StringComparison.OrdinalIgnoreCase))

Here's an example of how you can do it without the fancy LINQ and Lambda which seem to be confusing you:

Public Function FileMatches(folderPath As String, filePattern As String, phrase As String) As Boolean
    For Each fileName As String In Directory.GetFiles(folderPath, filePattern)
        If fileName.Contains(phrase) Then
            Return True
        End If
    Next
    Return False
End Function

Or, if you need it to be case insensitive:

Public Function FileMatches(folderPath As String, filePattern As String, phrase As String) As Boolean
    For Each fileName As String In Directory.GetFiles(folderPath, filePattern)
        If fileName.ToLower().Contains(phrase.ToLower()) Then
            Return True
        End If
    Next
    Return False
End Function

You would call the method like this:

lNewFace.Visible = FileMatches(path, "*.pdf", NewFace)

Try this:

 lNewFace.Visible = IO.Directory.GetFiles(path, "*.pdf").Where(Function(file) file. _
            Substring(2, 10) = NewFace).FirstOrDefault Is Nothing

Consider that the substring function will throw an exception if its arguments exceed the length of the string it is parsing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM