• 0

directory.getfiles method in VB.net


Question

Im using directory.getfiles in vb.net to try and get a list of files in a given path.

What i want is to return all that files that have certain extensions (*.xls and *.xml)

The search pattern im using is: "*.xls, *.xml" and this fails as it returns no files, when i know that there is. I've also tried using ";" and ":" to seperate the extensions but no luck.

Does anyone know how to check multiple extensions in the searchPattern argument??

thanks for reading...

Link to comment
https://www.neowin.net/forum/topic/158194-directorygetfiles-method-in-vbnet/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

you can use FileSystemObject which i use in .net because its very good at these kinda jobs

add reference to Microsoft scripting runtime i guess is the name

and then do this

? ?Private FS As New Scripting.FileSystemObjectClass
 ? ?Private Fol As Scripting.FolderClass
 ? ?Private Fil As Scripting.File
 ? ?Public Sub SearchDir(ByVal Dir As String)
 ? ? ? ?Fol = FS.GetFolder(Dir)
 ? ? ? ?For Each Fil In Fol.Files
 ? ? ? ? ? ?If Right(Fil.Name, 3) = "xls" Then
 ? ? ? ? ? ? ? ?'do something 
 ? ? ? ? ? ?End If
 ? ? ? ?Next
 ? ?End Sub

for more than 1 file format ... i suggest you put them in a string array just like Radium said and do an another nested loop to compar:)each file extension with the array item :)

  • 0

there's no reason he couldn't do the same exact thing with the built-in .NET stuff, which i'm assuming would be faster than using the FSO since its native to .NET and the FSO stuff is not.

and that still doesn't answer his question.

to my knowledge, there's no built-in way to do what you're asking, antny_uk. a quick google search seemed to imply you'll have to code it up yourself (a pretty easy task, just kinda annoying there's no built-in method)

  • 0
  Mave said:
you can use FileSystemObject which i use in .net because its very good at these kinda jobs

add reference to Microsoft scripting runtime i guess is the name

and then do this

? ?Private FS As New Scripting.FileSystemObjectClass
 ? ?Private Fol As Scripting.FolderClass
 ? ?Private Fil As Scripting.File
 ? ?Public Sub SearchDir(ByVal Dir As String)
 ? ? ? ?Fol = FS.GetFolder(Dir)
 ? ? ? ?For Each Fil In Fol.Files
 ? ? ? ? ? ?If Right(Fil.Name, 3) = "xls" Then
 ? ? ? ? ? ? ? ?'do something 
 ? ? ? ? ? ?End If
 ? ? ? ?Next
 ? ?End Sub

for more than 1 file format ... i suggest you put them in a string array just like Radium said and do an another nested loop to compar:)each file extension with the array item :)

Why use the Right(Fil.Name,3) to check for file extension, when VB.NET has a getFileExtension method call, from System.IO namespace.

  • 0

Dim xlsa As String()

Dim xmla As String()

Dim al As ArrayList

xlsa = Directory.GetFiles("*.xls")

xmla = Directory.GetFiles("*.xml")

al = New ArrayList(xlsa)

al.Add(xmla)

xlsa = Nothing

xmla = Nothing

'Now you have a nice ArrayList instead of an old array

'I wrote this off the top of my head, you may have to add a few parameters

'or change a few method names to get it to compile

'If you use C#.NET you may have to adapt this, shouldn't be too hard

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.