473,472 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Opening all files in a folder

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
Nov 21 '05 #1
1 9377
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" <vr****@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rob Mayo | last post by:
I know this seems like a noob question, but... Our group is working on a development server (Win2K Server, IIS 5.0) in which we are all Administors. On this server, we set up our projects by...
1
by: Bob Murdoch | last post by:
I've got an intranet application that presents a list of files in sort of a 'central repository' web page. Each file is an href in the form <a href=file://server/share/path/filename.ext>. When...
0
by: vinX | last post by:
Ok, i am making a file renaming utility, you can add filesize (in MB o KB), filepath, creation date and/or time, a counter, parent folder, th original name etc... I put the program in the...
2
by: Dan Sikorsky | last post by:
How can I stop the bin folder from automatically opening and showing all its contents upon startup, moving from project to project, etc.? It should only open when you click on it to expand it.
1
by: Ddraig | last post by:
Howdy, I was wondering if anyone could point me in the direction of some good code that will look at a folder of various text files, read in the names and then open them. For example opening...
4
by: Pieter | last post by:
Hi, From my VB.NET 2005 application I need to be able to open Files an Directory's with their default application: - A word-doc must open in word - A excell-document in Excel etc - in case...
11
by: moony marouane | last post by:
Hi all I'm looking for code regarding opening (not saving) binary files (pdf, word, excel...) from hard disk with asp, the website is hosted in C:\ and the pdf files are in D:\. Does anyone...
2
by: Jl_G_0 | last post by:
Hey all. Got a weird problem here. I need to open a folder that is on a server. The asp.net page and the IIS are on the same server. But i Cant access it from nowhere but locally. Simply because...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.