Results 1 to 6 of 6

Thread: [RESOLVED] Read contents of text files in a director into an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    23

    Resolved [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

  2. #2
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    23

    Re: Read contents of text files in a director into an array

    can you give me an example of some code?

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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))

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
  •  



Click Here to Expand Forum to Full Width