简体   繁体   English

如何检查文件名是否包含vb.net中字符串的一部分

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

I have a userform in 2008 vb express edition. 我在2008 vb express edition中有一个userform。 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. 部件号看起来像这个BX002(30x30)A1SS我想比较002(30x30)(只是文件名的这一部分)到一个目录中的所有文件。 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 下面的代码是C#中答案的翻译,但它也不起作用

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: 这是一个如何在没有花哨的LINQ和Lambda的情况下做到这一点的例子,这似乎让你感到困惑:

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 考虑如果子参数的参数超出了它正在解析的字符串的长度,则它将抛出异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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