Opening all files in a folder

V

Vandana Rola

Hello everyone,

I am a beginner in VB.net. I am trying to write a program which will
open around 300 files (which are .msg files)in a folder and extract
the responses of the questions in those files. I have been able to
open and extract data from one file using ...opentext(filename) method
but I am not able to figure out how to loop through each file in the
folder, open it, and extract data. I have been able to find a way to
extract those files in a list box. Is there any method to open those
file from listbox. Any help would be appreciated.

For your reference here is the code for the program:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("Surveys\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub

Private Sub AddButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles AddButton.Click
Dim objStreamwriter As System.IO.StreamWriter
Dim objStreamReader As System.IO.StreamReader
Dim strLine As String, intIndex, intNext, intAnswer, intFrom,
intDot, intAngle As Integer

Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

For Each dra In diar1

objStreamReader = System.IO.File.OpenText(?)
Do Until objStreamReader.Peek = -1
strLine = objStreamReader.ReadLine()
objStreamwriter =
System.IO.File.AppendText("studentSurvey.txt")

If (strLine.StartsWith("From")) Then
objStreamwriter.WriteLine()
intFrom = strLine.IndexOf("dl")
intDot = strLine.IndexOf(".")
objStreamwriter.Write(strLine.Substring(intFrom +
3, intDot - intFrom - 3))
objStreamwriter.Write(",")
End If
If ((strLine.StartsWith("Question") = True Or
strLine.StartsWith("Answer")) AndAlso UCase(strLine).EndsWith("<BR>")
= False) Then

If (strLine.StartsWith("Answer")) Then
strLine = objStreamReader.ReadLine()
objStreamwriter.Write(strLine)
objStreamwriter.Write(",")
End If

strLine = objStreamReader.ReadLine()
intIndex = strLine.IndexOf("[X]")
intAnswer = strLine.IndexOf("Answer")

If intIndex > 0 Then
intNext = strLine.IndexOf("[", intIndex + 1)
intAngle = strLine.IndexOf("<", intIndex + 1)
If intNext > 0 Then

objStreamwriter.Write(strLine.Substring((intIndex + 7), intNext -
intIndex - 7))
Else
If intAngle > 0 Then

objStreamwriter.Write(strLine.Substring((intIndex + 7), intAngle -
intIndex - 7))
End If

End If
objStreamwriter.Write(",")
End If

End If

objStreamwriter.Close()
Loop

objStreamReader.Close()
Next

End Sub
 
S

Sakharam Phapale

Hi Vandana,

Look at following example.

Thanks and Regards
Sakharam Phapale

C# Example

System.IO.DirectoryInfo di;
di = new System.IO.DirectoryInfo("D:\\Public");

foreach (System.IO.FileInfo diar in di.GetFiles())
{
listBox1.Items.Add (diar.FullName.ToString() );
}

VB Example

Dim di as System.Io.DirectoryInfo
di = new System.IO.DirectoryInfo("D:\Public")
Dim diar as System.IO.FileInfo
For Each diar In di.GetFiles()
listBox1.Items.Add (diar.FullName.ToString() );
Next


Vandana Rola said:
Hello everyone,

I am a beginner in VB.net. I am trying to write a program which will
open around 300 files (which are .msg files)in a folder and extract
the responses of the questions in those files. I have been able to
open and extract data from one file using ...opentext(filename) method
but I am not able to figure out how to loop through each file in the
folder, open it, and extract data. I have been able to find a way to
extract those files in a list box. Is there any method to open those
file from listbox. Any help would be appreciated.

For your reference here is the code for the program:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("Surveys\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub

Private Sub AddButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles AddButton.Click
Dim objStreamwriter As System.IO.StreamWriter
Dim objStreamReader As System.IO.StreamReader
Dim strLine As String, intIndex, intNext, intAnswer, intFrom,
intDot, intAngle As Integer

Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

For Each dra In diar1

objStreamReader = System.IO.File.OpenText(?)
Do Until objStreamReader.Peek = -1
strLine = objStreamReader.ReadLine()
objStreamwriter =
System.IO.File.AppendText("studentSurvey.txt")

If (strLine.StartsWith("From")) Then
objStreamwriter.WriteLine()
intFrom = strLine.IndexOf("dl")
intDot = strLine.IndexOf(".")
objStreamwriter.Write(strLine.Substring(intFrom +
3, intDot - intFrom - 3))
objStreamwriter.Write(",")
End If
If ((strLine.StartsWith("Question") = True Or
strLine.StartsWith("Answer")) AndAlso UCase(strLine).EndsWith("<BR>")
= False) Then

If (strLine.StartsWith("Answer")) Then
strLine = objStreamReader.ReadLine()
objStreamwriter.Write(strLine)
objStreamwriter.Write(",")
End If

strLine = objStreamReader.ReadLine()
intIndex = strLine.IndexOf("[X]")
intAnswer = strLine.IndexOf("Answer")

If intIndex > 0 Then
intNext = strLine.IndexOf("[", intIndex + 1)
intAngle = strLine.IndexOf("<", intIndex + 1)
If intNext > 0 Then

objStreamwriter.Write(strLine.Substring((intIndex + 7), intNext -
intIndex - 7))
Else
If intAngle > 0 Then

objStreamwriter.Write(strLine.Substring((intIndex + 7), intAngle -
intIndex - 7))
End If

End If
objStreamwriter.Write(",")
End If

End If

objStreamwriter.Close()
Loop

objStreamReader.Close()
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top