• 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

    • Not having a native app I guess is not good. That said when I have to use Windows and I want to use AI I do not use co-pilot. Lots of AI apps work on lots of OS’es. Just install the AI app you want on iOS or MacOs. Also we are in early days. In 3 years time suspect lots of these AI apps will be gone either by not making it or being bought up by others.
    • Gemini can now catch you up on the latest changes to your files in Google Drive by David Uzondu Google recently announced a new feature for Drive designed to help you keep track of what has changed in your files without having to dig through them manually. The feature, called "Catch me up", shares its name with a similar tool in Google Chat, which helps users quickly catch up on conversations without scrolling through long message threads. With the new feature, Gemini will now attempt to identify files in your Drive that have been altered since you last opened them, offering a summary of these changes. This aims to save you the headache of figuring out what is new or crucial. This covers edits made to Docs and also new comments left on Docs, Sheets, and Slides. Google clarifies that this summary is not exhaustive but is meant to point out potentially important updates. To use it, you can find a "Catch me up" prompt on the Google Drive Home page, and clicking it will bring up the summary in the Gemini side panel. Alternatively, for individual files, you will see an activity indicator on documents that have likely changed. You can click this indicator directly or select the file and hit a "catch me up" button in the selection toolbar to get the lowdown. The feature is currently available in English and has begun rolling out to users on both Rapid Release and Scheduled Release domains. It could take up to 15 days for everyone eligible to see it. It is coming to Google Workspace accounts, including Business Standard and Plus, Enterprise Standard and Plus, as well as customers with a Gemini Education or Gemini Education Premium add-on, and Google One AI Premium subscribers. Users who previously bought the Gemini Business or Gemini Enterprise add-ons before those were discontinued for new sales on January 15, 2025, will also get access.
    • Hehehe.. So, I realise my post came rather late, but did you actually try building one yet? 
    • Oh shoot. Could this be because of the OpenAI sale?
    • Not gonna lie, turned me on
  • Recent Achievements

    • Week One Done
      mywakehealth earned a badge
      Week One Done
    • Dedicated
      jbatch earned a badge
      Dedicated
    • Week One Done
      Leonard grant earned a badge
      Week One Done
    • One Month Later
      portacnb1 earned a badge
      One Month Later
    • Week One Done
      portacnb1 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      284
    2. 2
      snowy owl
      158
    3. 3
      +FloatingFatMan
      149
    4. 4
      ATLien_0
      140
    5. 5
      Xenon
      130
  • Tell a friend

    Love Neowin? Tell a friend!