阅读文件夹中的多个文本文件 [英] Read multiple text files in a folder

查看:140
本文介绍了阅读文件夹中的多个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹中的许多文本文件。我现在能做的就是通读一个文本的时间,并将其插入到数据库中。我的小应用程序读取,当我调试它的文本文件。所以,我需要跑几次才能阅读所有的文本文件,并将其导入到数据库中。

I have many text files in a folder. What I can do now is to read through one text at a time and insert it into the database. My little app reads a text file when I debug it. So, I need to run it several times to read all those text files and import them into the database.

我的问题是如何读取多个文本文件的文件夹内的时间。这是我的code,工作正常,但在同一时间只能读取一个文本文件。

My question is how to read multiple text files inside a folder at a time. Here's my code which works fine but it reads only one text files at a time.

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

        Dim filelocation As String
        filelocation = "F:\txtfiles\ch25.txt"
        Dim chid As Integer
        chid = 25



        'read from file'
        Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
        Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
        MyStream.Close()



        Dim count As Integer

        'insert text to table'
        For d As Integer = 0 To vArray.Length - 1 Step 1

            If d = vArray.Length - 1 Then
                Exit For
            End If

            InsertKh(chid, d + 1, vArray(d))
            count = d + 1
        Next


       MsgBox ("Done Inserting")

End Sub

很显然,我需要一种方法来通过一个文件夹循环,并检查是否有文本文件。但我不能得到它的权利。任何人都可以给我一些code或链接?我使用VB.NET,.NET 3.5

Obviously, I need a way to loop through a folder and check if there's text file. But I cant get it right. Can anyone show me some code or links ? I'm using VB.NET, .NET 3.5

太感谢了。

推荐答案

。看看<一href="http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles.aspx">Directory.GetFiles.

您可以用类似的* .txt指定的搜索模式调用它来查找特定类型的文件。事情是这样的:

You can call it with a specified search pattern like "*.txt" to find a specific kind of file. Something like this:

    Dim fileEntries As String() = Directory.GetFiles(targetDirectory,"*.txt")
    ' Process the list of .txt files found in the directory. '
    Dim fileName As String
    For Each fileName In fileEntries
        ProcessFile(fileName)

这篇关于阅读文件夹中的多个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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