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
Solved

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

Posted on 2008-10-15
6
170 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get row value in vb.net 4 17
VB.net and sql server 4 45
Help with opening combobox using VB.NET. 3 25
Error message when using PrintSocket.Send 7 18
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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