Reputation: 99
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 save the final output into the save path, again listed in a text file. Any help is greatly appreciated. ReplyQuote
Upvotes: 1
Views: 198
Reputation: 19308
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.
Upvotes: 1
Reputation: 1294
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
Upvotes: 4
Reputation: 31610
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();
}
}
Upvotes: 0
Reputation: 49988
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.
Upvotes: 1