Solved

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

Posted on 2008-10-15
6
166 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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