• 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.
  • Posts

    • Sony announces new State of Play games showcase for tomorrow by Pulasthi Ariyasinghe Game publishers love to get their showcases and announcements to happen in June, and following many other companies, Sony has now thrown its hat into the ring as well. There's not long to wait either. The PlayStation maker will be hosting a showcase of its own tomorrow, June 4, focused on titles coming to its platforms. The June State of Play will go live on Wednesday, June 4, at 2pm PT / 5pm ET / 11pm CEST. Fans can catch the show across Sony's YouTube and Twitch channels live. It seems that unlike previous State of Play shows, a Japanese broadcast will not be happening, with only an English stream being confirmed this time. "Watch live for news and updates on must-play games coming to PS5," says the announcement today. "The show highlights a selection of great games from creators across the globe." Other than that, Sony has not given any hints or teasers about what fans can expect to see at the show. We still may get new looks at upcoming first-party titles like Ghost of Yotei from Ghost of Tsushima developer Sucker Punch Productions, Marathon from Destiny studio Bungie, and maybe even Insomniac’s Wolverine title that's set in its Spider-Man universe. Sony still has some live service titles in development that may get some time in the spotlight here too. As always, more PC ports have a chance of being revealed here as well, continuing Sony's multiplatform venture. Following the State of Play showcase, Summer Game Fest will officially kick off with its own event later this week. Microsoft is also preparing to go live with its own Xbox Games Showcase, with a separate Direct stream focused on its next RPG release, The Outer Worlds 2 from Obsidian Entertainment.
    • Hello All Every day MS App store lists the same apps with update available,  select update all, and then think all is fine, and next day same list shows up again.  Any advice on how to fix this for good?   Should i resort to clean install? On the AMD Ryzen 7 7700X Desktop    
    • Battery life for devices like this should - at least - a week. It's not a bl**dy phone you need to charge every day. 'rolls eyes'
  • Recent Achievements

    • Week One Done
      Adam Todd earned a badge
      Week One Done
    • Contributor
      Ed B went up a rank
      Contributor
    • One Month Later
      moporcho earned a badge
      One Month Later
    • One Month Later
      Parotel earned a badge
      One Month Later
    • Reacting Well
      Cryptecks earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      195
    2. 2
      snowy owl
      139
    3. 3
      ATLien_0
      132
    4. 4
      Xenon
      120
    5. 5
      +FloatingFatMan
      104
  • Tell a friend

    Love Neowin? Tell a friend!