如何一次在目录中的多个文本文件中搜索一串文本 [英] How to search multiple text files in a directory for a string of text at once

查看:69
本文介绍了如何一次在目录中的多个文本文件中搜索一串文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一定数量项目的列表框.
对于 ListBox 中的每个项目,文件目录中都存在相应的文本文件.

I have a ListBox with a certain amount of items in it.
For each item in the ListBox a corresponding text file exists in the file directory.

我需要在每个文本文件(基于列表框中的内容)中搜索人名.每个文本文件可能包含名称,也可能不包含.
然后我想返回包含名称的文本文件.

I need to search each text file (based on what's in the ListBox) for a persons name. Each text file may contain the name or it may not.
I would then like a return which text file contains the name.

我已尝试将此作为搜索文本文件的一种方式:它有效,但我不确定如何根据 ListBox 中的内容重复此操作.

I have tried this as a way to search a text file: it works, but I'm not sure how to get this to repeat based on whats in a ListBox.

Dim sFileContents As String = String.Empty
If (System.IO.File.Exists((Application.StartupPath) & "\Project_Green.txt")) Then
    sFileContents = (System.IO.File.ReadAllText((Application.StartupPath) & "\Project_Green.txt"))
End If
If sFileContents.Contains(TextBox4.Text) Then
    MessageBox.Show("yup")
Else
    MessageBox.Show("nope")
End If

另外,如果可以忽略大小写那就太好了.

Also, if it would be possible to ignore case that would be great.

推荐答案

如果您在一个目录中有一堆文件,并且您在 ListBox 中有它们的名称,并且您想搜索它们的内容以查找某些内容.

If you have a bunch of files in a directory and you have their names in a ListBox, and you want to search their contents for something.

单班轮查询:

Imports System.IO
'...

Sub TheCaller()
    Dim dir = My.Application.Info.DirectoryPath
    Dim ext = ".txt" ' If the extensions are trimmed in the list.
    Dim find = TextBox4.Text
    Dim files = Directory.EnumerateFiles(dir).Where(Function(x) ListBox1.Items.Cast(Of String).
        Any(Function(y) String.Concat(y, ext).
        Equals(Path.GetFileName(x),
        StringComparison.InvariantCultureIgnoreCase) AndAlso File.ReadLines(x).
        Any(Function(z) z.IndexOf(find, StringComparison.InvariantCultureIgnoreCase) >= 0))).ToList

    ListBox2.Items.Clear()
    ListBox2.Items.AddRange(files.Select(Function(x) Path.GetFileNameWithoutExtension(x)).ToArray)
End Sub

或者,如果您更喜欢 For Each 循环:

Or if you prefer the For Each loop:

Sub Caller()
    Dim dir = My.Application.Info.DirectoryPath
    Dim find = TextBox4.Text
    Dim files As New List(Of String)

    For Each f As String In ListBox1.Items.Cast(Of String).
        Select(Function(x) Path.Combine(dir, $"{x}.txt"))

        If File.Exists(f) AndAlso
        File.ReadLines(f).Any(Function(x) x.IndexOf(find,
                                StringComparison.InvariantCultureIgnoreCase) <> -1) Then
            files.Add(f)
        End If
    Next

    ListBox2.Items.Clear()
    ListBox2.Items.AddRange(files.Select(Function(x) Path.GetFileNameWithoutExtension(x)).ToArray)
End Sub

无论哪种方式,files 列表都包含匹配项(如果有).

Either way, the files list contains the matches if any.

这篇关于如何一次在目录中的多个文本文件中搜索一串文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆