Getting files of 2 extensions

G

Guest

Hi,
I want to write a For loop that will put file names of extensions (*.txt and
*.csv) in an array.

I had it work fine for one extension, and I need help making it work for two
extensions. My code is below:

Dim dir As New DirectoryInfo("C:\Test")
Dim fileNames(dir.GetFiles("*.txt").Length - 1)) As String
Dim myfile As FileInfo
For Each myfile In dir.GetFiles("*.txt")
fileNames(i) = myfile.FullName
i += 1
Next
 
G

Guest

Amjad,

For Each myfile In dir.GetFiles
If myfile.Extension = ".txt" Or myfile.Extension = ".cvs" Then
fileNames(i) = myfile.FullName
i += 1
End If
Next

Kerry Moorman
 
H

Herfried K. Wagner [MVP]

Amjad said:
I want to write a For loop that will put file names of extensions (*.txt
and
*.csv) in an array.

I had it work fine for one extension, and I need help making it work for
two
extensions. My code is below:

Dim dir As New DirectoryInfo("C:\Test")
Dim fileNames(dir.GetFiles("*.txt").Length - 1)) As String
Dim myfile As FileInfo
For Each myfile In dir.GetFiles("*.txt")
fileNames(i) = myfile.FullName
i += 1
Next

You may want to call 'GetFiles' twice, one time for each extension.
 
G

Guest

Jan. 10, 2005

I was working on this as the others were posting! :) It's nice to get
lots of replies! Try:

public sub button1_click(...) ...
dim files as arraylist
dim exts() as string = {"*.txt", "*.rtf"}
arr = GetFiles(exts)
' Do something with arr
end sub

Public Function GetFiles(byval ext() as string) as ArrayList
dim Files as new arraylist
dim extension as string
dim dir as new directoryinfo("C:\Test")
dim myFile as fileinfo

for each extension in ext 'Gets Files For Each Extension Passed
for each myFile in dir.getfiles(extension) ' Adds Files To
ArrayList
files.add(myfile.fullname)
next
next
End Function

I didn't test this, and I hope this will still help you! :) Have a
great day! (Tip: If you have a problem hardcoding the size before you find
out how big it needs to be, then use an ArrayList. ArrayLists allow unlimited
calls to .Add without you having to specify the size of the list!)


Joseph MCAD
 
G

Guest

Amjad,

In my reply I forgot that you are dimensioning your array like this:

Dim fileNames(dir.GetFiles("*.txt").Length - 1)) As String

You could probably dimension it like this:

Dim fileNames(dir.GetFiles("*.txt").Length + dir.GetFiles("*.csv").Length -
1)) As String

Or you could use an arraylist and not worry about array sizes and indexes.

Kerry Moorman
 
J

Jay B. Harlow [MVP - Outlook]

Amjad,
In addition to the other comments:

I would use Directory.GetFiles as it returns an array of Strings, as opposed
to DirectoryInfo.GetFiles that returns an array of FileInfo objects.
Dim fileNames() As String
fileNames = Directory.GetFiles("C:\Test", "*.txt")

If I needed two or more file extensions combined then I would call the
function twice.

Dim fileNames1() As String
fileNames1 = Directory.GetFiles("C:\Test", "*.txt")

Dim fileNames2() As String
fileNames2 = Directory.GetFiles("C:\Test", "*.csv")

Dim fileNames(fileNames1.Length + fileNames2.Length - 1) As String
Array.Copy(fileNames1, 0, fileNames, 0, fileNames1.Length)
Array.Copy(fileNames2, 0, fileNames, fileNames1.Length,
fileNames2.Length)

Here I used Array.Copy to combine the two arrays, I could have used an
ArrayList instead & used AddRange.

Dim fileNames As New ArrayList
fileNames.AddRange(fileNames1)
fileNames.AddRange(fileNames2)

In either case I would consider generalizing the above into a loop as Joseph
showed.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top