Solved

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

Posted on 2008-10-15
6
143 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 69

Expert Comment

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

Author Comment

by:stewdaddy
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
why do you store an array in an array. what do you want to do exactly?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:stewdaddy
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 125 total points
Comment Utility
>>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
Comment Utility
Thanks for your help.  It is now working properly.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now