473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting files of 2 extensions

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

Nov 21 '05 #1
5 1289
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


"Amjad" wrote:
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

Nov 21 '05 #2
"Amjad" <Am***@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

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
"Amjad" wrote:
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

Nov 21 '05 #4
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
"Kerry Moorman" wrote:
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


"Amjad" wrote:
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

Nov 21 '05 #5
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
"Amjad" <Am***@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com... 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

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Georgy Pruss | last post by:
On Windows XP glob.glob doesn't work properly for files without extensions. E.g. C:\Temp contains 4 files: 2 with extensions, 2 without. C:\Temp>dir /b * aaaaa.aaa bbbbb.bbb ccccc ddddd ...
5
by: hokiegal99 | last post by:
Hi, I have a working Python script that renames files that don't currently have PC based file extensions. For example, if there is a MS Word file that does not have '.doc' on the end of it, the...
2
by: Steve Teeples | last post by:
I am using the Filter property within OpenFileDialog. I understand how to filter files with extensions, but this time I need to filter files without extensions. Can someone tell me how this is...
2
by: John Bowman | last post by:
Hi All, ..NET 1.1... I'm wondering if there is any approach more convenient to get a list of FileInfo objects than the following. For example, if I wanted to get 1 list of all the Exe's and all...
2
by: Ames111 | last post by:
Hi i want to be able to determine what type of file is being dragged on to my form, ive got this: private void Form1_DragDrop(object sender, DragEventArgs e) { //tell me the file type please ...
1
by: iwdu15 | last post by:
hi, how can i get the icon associated with a certain file type? thanks -- -iwdu15
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
4
by: Tom Dacon | last post by:
Google groups didn't offer any help on this, and I tried this in the vsnet.ide newsgroup but didn't get a response. I'm trying it here in hopes that someone might have a clue: The company I work...
3
by: Davo1977 | last post by:
Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.