473,771 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ 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(fil ename) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInf o("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.EventArg s) Handles AddButton.Click
Dim objStreamwriter As System.IO.Strea mWriter
Dim objStreamReader As System.IO.Strea mReader
Dim strLine As String, intIndex, intNext, intAnswer, intFrom,
intDot, intAngle As Integer

Dim di As New IO.DirectoryInf o("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("stu dentSurvey.txt" )

If (strLine.Starts With("From")) Then
objStreamwriter .WriteLine()
intFrom = strLine.IndexOf ("dl")
intDot = strLine.IndexOf (".")
objStreamwriter .Write(strLine. Substring(intFr om +
3, intDot - intFrom - 3))
objStreamwriter .Write(",")
End If
If ((strLine.Start sWith("Question ") = True Or
strLine.StartsW ith("Answer")) AndAlso UCase(strLine). EndsWith("<BR>" )
= False) Then

If (strLine.Starts With("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((intI ndex + 7), intNext -
intIndex - 7))
Else
If intAngle > 0 Then

objStreamwriter .Write(strLine. Substring((intI ndex + 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 9395
Hi Vandana,

Look at following example.

Thanks and Regards
Sakharam Phapale

C# Example

System.IO.Direc toryInfo di;
di = new System.IO.Direc toryInfo("D:\\P ublic");

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

VB Example

Dim di as System.Io.Direc toryInfo
di = new System.IO.Direc toryInfo("D:\Pu blic")
Dim diar as System.IO.FileI nfo
For Each diar In di.GetFiles()
listBox1.Items. Add (diar.FullName. ToString() );
Next
"Vandana Rola" <vr****@yahoo.c om> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
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(fil ename) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInf o("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.EventArg s) Handles AddButton.Click
Dim objStreamwriter As System.IO.Strea mWriter
Dim objStreamReader As System.IO.Strea mReader
Dim strLine As String, intIndex, intNext, intAnswer, intFrom,
intDot, intAngle As Integer

Dim di As New IO.DirectoryInf o("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("stu dentSurvey.txt" )

If (strLine.Starts With("From")) Then
objStreamwriter .WriteLine()
intFrom = strLine.IndexOf ("dl")
intDot = strLine.IndexOf (".")
objStreamwriter .Write(strLine. Substring(intFr om +
3, intDot - intFrom - 3))
objStreamwriter .Write(",")
End If
If ((strLine.Start sWith("Question ") = True Or
strLine.StartsW ith("Answer")) AndAlso UCase(strLine). EndsWith("<BR>" )
= False) Then

If (strLine.Starts With("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((intI ndex + 7), intNext -
intIndex - 7))
Else
If intAngle > 0 Then

objStreamwriter .Write(strLine. Substring((intI ndex + 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
1330
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 first creating the appropriate folder on Under C:\Inetpub\wwwroot\<ProjectName>. Then we create a virtual directory in IIS pointing to that folder. Then we create a new project on our own machines (Win2K) using FrontPage Extensions. For each of...
1
6937
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 the user clicks on an excel file, it is opening within the browser, which unforntuntely confuses the heck out of them because the usual Print/Print Preview menu options are not available. I would like the user to be presented with the typical...
0
1949
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 (registry: *.*\shell\myprogram\command(valu of the comand key is "...\myprogram.exe" "%1") context menu, so tha when you select multiple files, the paths would be passed to th program. First i was having problems with multiple instances of my program, an...
2
1128
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
1262
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 various log files would be a good example of something like this. I think I've got the whole reading the text out of the file figured out, but just wondering about how to select files in a folder, execute the streamreader code, then move on to the...
4
2174
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 it's a folder it must open the Folder No problem there, but when I want to open a file without extension, that hasn't offcourse a default application, I'm getting an error. I'd prefer to
11
2082
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 have an idea about this?? Many thanks in advance. Moony.
2
1596
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 the link directs to eg. E:\Files\Stuff ... and when any computer tries to access it, it searches on itself and not on the server. Tried to create the folder (virtual folder) inside the ASP folder on IIS (enabled Allow File Browsing too) , but...
10
3690
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 "single" file with my app which works to get commandline arguments to get file paths: Dim cla As String() = Environment.GetCommandLineArgs() If cla.Length 1 Then
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.