?
Solved

How do I read the contents of a folder with a specific extension into an array?

Posted on 2008-10-15
6
Medium Priority
?
194 Views
Last Modified: 2012-05-05
I need to read a directory and pull of the files with an extension of .wfd in to an array.  
0
Comment
Question by:stewdaddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22722952
You can use Directory.GetFiles as shown in http://msdn.microsoft.com/en-us/library/wz42302f.aspx
0
 

Author Comment

by:stewdaddy
ID: 22723816
Based on that info, my function looks like this:
    Public Function WFD_List()
        Dim strWFDDirectory As String = ("J:\" & txtProject.Text & "\Pnet\")
        Dim i As Integer = 0
        Dim a As Integer = System.IO.Directory.GetFiles(strWFDDirectory, "*.wfd").Length
        Dim arrWFDFiles(a, 3)
        Dim strWFDFileList As String() = System.IO.Directory.GetFiles(strWFDDirectory, "*.wfd")
        Dim dir As String

        For Each dir In strWFDFileList
            arrWFDFiles(i, 0) = IO.Directory.GetFiles(strWFDDirectory, "*.wfd")
            i = i + 1
        Next

        Return arrWFDFiles
    End Function

However, when I do the following in another sub I receive an "InvalidCastException was unhandled" error:
        Dim arrWFD As Object(,) = WFD_List()
        MessageBox.Show(arrWFD(0, 0))
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22723935
why do you store an array in an array. what do you want to do exactly?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stewdaddy
ID: 22724071
I want every file within the directory that has a .WFD extension put in to an array.
The WFD file name will look like this: JS_12345_001.wfd
I want a multidimensional array that consists of:
array(0,0) = full path and file name (ex. J:\12345\JS_12345_001.wfd)
array(0,1) = just the first portion of the file name (ex. JS)
array(0,2) = just the second part of the file name (ex. 12345)
array(0,3) = just the third part of the file name (ex. 001)
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 22724253
>>I want every file within the directory that has a .WFD extension put in to an array.

Look at the example again. The GetFiles method already return an array of string.

Dim dirs As String() = Directory.GetFiles(strWFDDirectory, "*.wfd")
'Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
Dim arrWFDFiles(dirs.length, 3)
dim i as integer
For Each dir as string In dirs
    arrWFDFile(i, 0) = dir
    arrWFDFile(i, 1) = something you need to do here
    arrWFDFile(i, 2) = something you need to do here
    arrWFDFile(i, 3) = something you need to do here
    i = i + 1
'                Console.WriteLine(dir)
Next
0
 

Author Closing Comment

by:stewdaddy
ID: 31506363
Thanks for your help.  It is now working properly.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question