• 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.
  • Popular Now

  • Posts

    • considering that took 4 years "to fix" ryzen performance on windows 11. look at the amount of time that AMD user have a degraded performance compared to intel on microsoft latest and greatest OS
    • MosaLingua Language Learning lifetime subscription price dropped by 97% by Steven Parker Today's highlighted deal lets you save 97% off lifetime access to a MosaLingua Language Learning Fluency Bundle. Buying link below. Description: Learning new languages doesn't have to be difficult or exhausting. In fact, you can learn and speak a foreign language at your own pace. MosaLingua is an all-in-one platform for learning the most useful words and expressions in the language of your choice, practicing language immersion, and improving your language skills with numerous different resources. Pick up the skills you need to use the language in any situation and store up to 6,000 keywords and expressions in your long-term memory, and choose content depending on your needs and your level. MosaLingua is a personalized language learning tool that adjusts to your level and your schedule, letting you make quick progress and avoid time-wasters. Access to 11 Full Language Courses: English, Spanish, French, Italian, German, Portuguese, Russian, Chinese, Japanese, Korean, and Arabic—plus any new languages we add in the future, at no extra cost. Science-Based Learning Method: Spaced repetition, neuroscience, cognitive psychology, and the 80/20 rule combine to help you retain vocabulary & speak faster. All-in-One Learning App: Practice listening, speaking, reading, and writing in one place with dialogues, flashcards, videos, audiobooks, grammar lessons & hands-on exercises. AI-Powered Coaching & Tools: Practice with MosaChat-AI, your 24/7 virtual tutor that helps you improve your writing and speaking skills with personalized feedback. Learn from the Content You Love: Thanks to MosaDiscovery, you can turn any online content into a learning opportunity. Learn languages by watching YouTube videos, Netflix series, reading articles, or exploring any website—with instant translations, personalized flashcards, and real-world vocabulary. Offline Mode & Cross-Platform Sync: Learn on your terms—anywhere, anytime. Use the app on iOS, Android, or the web. Built for Busy People Who Want Quick Results: Make real progress with just 10 minutes per day. Short, targeted sessions fit into any schedule. Good to know Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: desktop or mobile Max number of device(s): 1 Only available to NEW users Version: latest Updates included Lifetime access to MosaLingua Language Learning normally costs $4,850, but you can pick it up for just $97.99 for a limited time that's a saving of $4,702 off. For a full description, specs, and info, click the link below. Mosalingua: Lifetime Subscription now just $97.99 (was $4,850) Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • you know what, looking at it again. you're right, i'm wrong. there are no curves. it's just straight lines. it's flat.
  • Recent Achievements

    • One Month Later
      EdwardFranciscoVilla earned a badge
      One Month Later
    • One Month Later
      MoyaM earned a badge
      One Month Later
    • One Month Later
      qology earned a badge
      One Month Later
    • One Year In
      Frinco90 earned a badge
      One Year In
    • Apprentice
      Frinco90 went up a rank
      Apprentice
  • Popular Contributors

    1. 1
      +primortal
      449
    2. 2
      +FloatingFatMan
      249
    3. 3
      snowy owl
      239
    4. 4
      ATLien_0
      196
    5. 5
      Xenon
      141
  • Tell a friend

    Love Neowin? Tell a friend!