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

五迷三道 提交于 2019-12-11 04:51:43

问题


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))

回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/11229874/how-can-i-check-if-filename-contains-a-portion-of-a-string-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!