473,761 Members | 8,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.G etFiles("*.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 1312
Amjad,

For Each myfile In dir.GetFiles
If myfile.Extensio n = ".txt" Or myfile.Extensio n = ".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.G etFiles("*.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***@discussi ons.microsoft.c om> 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.G etFiles("*.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(ex tension) ' Adds Files To
ArrayList
files.add(myfil e.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.G etFiles("*.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.G etFiles("*.txt" ).Length - 1)) As String

You could probably dimension it like this:

Dim fileNames(dir.G etFiles("*.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.Extensio n = ".txt" Or myfile.Extensio n = ".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.G etFiles("*.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.GetFi les as it returns an array of Strings, as opposed
to DirectoryInfo.G etFiles that returns an array of FileInfo objects.
Dim fileNames() As String
fileNames = Directory.GetFi les("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.GetFi les("C:\Test", "*.txt")

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

Dim fileNames(fileN ames1.Length + fileNames2.Leng th - 1) As String
Array.Copy(file Names1, 0, fileNames, 0, fileNames1.Leng th)
Array.Copy(file Names2, 0, fileNames, fileNames1.Leng th,
fileNames2.Leng th)

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.AddRa nge(fileNames1)
fileNames.AddRa nge(fileNames2)

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

Hope this helps
Jay
"Amjad" <Am***@discussi ons.microsoft.c om> wrote in message
news:3C******** *************** ***********@mic rosoft.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.G etFiles("*.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
3085
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 C:\Temp>dir /b *.
5
2035
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 script will append that. The script also knows to *not* add an extension if the file already has one. Bascially, here's how I'm doing it: for fname in files: doc_id = string.find(file(os.path.join(root,fname),
2
3791
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 done? -- Steve
2
2513
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 the Dll's and all the Txt's in a folder, if appears I need to do something like the following: ArrayList InterestingFiles = new ArrayList(); DirectoryInfo di = new DirectoryInfo(@"C:\My Folder");
2
2582
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 } private void Form1_DragEnter(object sender, DragEventArgs e) {
1
2431
by: iwdu15 | last post by:
hi, how can i get the icon associated with a certain file type? thanks -- -iwdu15
13
4901
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, suggestions or improvements? if( wmv file) document.write("<OBJECT id=Player classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 height="354" width="479">
4
1514
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 for uses a couple of non-standard file name extensions for files containing SQL: .vw for views, .prc for stored procedures. The VS IDE doesn't recognize these as SQL files, so it doesn't do syntax coloring like it does for files with the...
3
2887
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 a directory but have slightly different extensions such as .txt,. TXT, or text. This expression should show how to rename them to all have .txt extensions. I understand i will nedd the character class so it contains a hypen, dot, slash and w+...
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.