开发者

Newbie Question: Read and Process a List of Text Files

I'm completely new to .NET and am trying as a first step to write a text processing program. The task is simple: I have a list of 10,000 text files stored in one folder, and I'm trying to read each one, store it as a string variable, then run it through a series of functions, then save the final output to another folder. So far I can only manage to manually input the file path like this (in VB.NET):

 Dim tRead As System.IO.StreamReader

    Public Function ReadFile() As String

        Dim EntireFile As String

        tRead = File.OpenText("c:\textexample\00001.txt")

        EntireFile = tRead.ReadToEnd

        Return EntireFile

    End Function

 Public Function Step1()
  .....
 End Function

 Public Function Step2()
  .....
 End Function
  ..............

I'm wondering, therefore, if there's a way to automate this process. Perhaps for example store all input file path into a text file then read each entry at a time, then s开发者_Go百科ave the final output into the save path, again listed in a text file. Any help is greatly appreciated. ReplyQuote


You're making it too complicated, it's only 3 lines of code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Files() As String = IO.Directory.GetFiles("C:\")
    For Each File As String In Files
        Process(IO.File.ReadAllText(File))
    Next
End Sub

Public Sub Process(ByVal s As String)
End Sub


You could just read all the files from the directory instead using Directory.GetFiles. See the example at the bottom of the page. Then use File.ReadAllText to read all the text in the file to a string.


You could combine a couple of System.IO classes with the Select extension method to read the files into an IEnumerable, like so...

Directory.GetFiles(myDir, "*.txt").Select(_
    Function(fileName) File.ReadAllText(fileName))

You could even extend the lambda expression you pass to Select to do whatever processing is needed.


Something to get you started:

   var files = from file in new DirectoryInfo(@"YourPath").GetFiles()
                    select file;

        foreach (var file in files)
        {
            using (var reader = new StreamReader(file.FullName))
            {
                // Do Stuff
                //string contents = reader.ReadToEnd();
            }

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜