|
-
Jun 16th, 2015, 10:03 PM
#1
Thread Starter
Junior Member
[RESOLVED] Read contents of text files in a director into an array
Hi
I'm making a program in Visual Basic, and i'm trying to find out how to read the contents of a directory into an array. In a loaction, for example, "C:\samplefolder", there are an unspecified number of text files, and the contents of these files need to be read into an array. I had an idea where an the program goes through the directory and enters the file name of each text file into an array called "filename()", then using the information stored in the "filename()" array, basic text file processing could be used to extract the contents of the array i.e. FileOpen(Filenum, filename(x), OpenMode.Input). The only problem is
a) I don't know how to get the program to go thorugh the directory and get the file names
b) I don't know how to write the contents of the text files into an array, although i do no how to insert it into a text box
It would be greatly appreciated if you could help me
thanks
-
Jun 16th, 2015, 10:11 PM
#2
Hyperactive Member
Re: Read contents of text files in a director into an array
Use the FileSystem object and a For each File in Directory loop to iterate through the files. For each file open up a read stream and store the contents in the Array.
-
Jun 16th, 2015, 10:14 PM
#3
Thread Starter
Junior Member
Re: Read contents of text files in a director into an array
can you give me an example of some code?
-
Jun 17th, 2015, 12:36 AM
#4
Hyperactive Member
Re: Read contents of text files in a director into an array
There are thousands of examples of how to use the FileSystem object all over the net and this PC doesn't have VB on it to write new examples with. Just search you will find it.
-
Jun 17th, 2015, 01:23 AM
#5
Re: Read contents of text files in a director into an array
You can call the IO.Directory.GetFiles method to get an String array containing the paths of all the files in a folder. It will allow you to filter list by extension too. You can then loop through that array and read each file. The IO.File.ReadAllText method will read the entire contents of a file into a single String. If you create an array of the same size as the file list, you can use a For loop and then read the contents of each file into the same index as the file path.
-
Jun 18th, 2015, 01:37 PM
#6
Re: Read contents of text files in a director into an array
it's just 1 line of code:
Code:
Dim contentsArray() As String = Array.ConvertAll(IO.Directory.GetFiles("path", "*.txt"), Function(n) IO.File.ReadAllText(n))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|