Solved

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

Posted on 2008-10-15
6
176 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
  • 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
Industry Leaders: 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 125 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

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!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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