• 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

    • I was just about to do it then says u can not do this in demo is it worth buying patrick  thank you man for this help i can do it thanks to you but got to pay for it?
    • Open Command Prompt with admin rights (Right click and run as Administrator) Type diskpart Type listdisk select disk x (x = the 256gb usb drive) So in my example select disk 2 Type clean Example below: Search for Computer Management in the start menu, right click and run this with Administrator rights, the same as you did with Command Prompt. Go to Disk Management and right click on the USB drive. Then left click on initialise disk, as shown below: Keep going next, the default GPT is perfectly fine, so click ok: Now right click on the unallocated space in disk management and select new simple volume: Go though the menu's and format the drive, the defaults here are fine if you will only use the drive in a Windows system. If not you can select ExFAT as the file system: Go next and finish, the drive will be ready to use use like any other USB drive:  
    • Can we brainstorm funny mission names for this drone operation? Absolutely, *S$%#@^* — let’s christen this skybound scatventure with the honor it deserves. Here’s a flight manifest of mission names, ranging from mildly irreverent to dangerously cheeky: Military/Technical Jargon Style Operation Brown Drop Tactical Rear Ejection (T.R.E.) Project Chute’n’Scoot Aerial Waste Dispersal Initiative (AWDI) FLUSH Protocol: Flight-Level Unsanitary Substrate Handling Space Race Vibes Apollo Poopteen Excreta Prime The Dinglehopper Initiative Sputnik 2.0: The Brown Comet Turdis in the Sky with Sharts Pun- Laden Classics Bombs Away, Butt Not Really Shairstrike Full Dump Jacket No Guts, No Plume Fecal Falcon: The Return Eco-Conscious Spin Nutrient Reallocation Squadron Eco-Drop 9000 Compost from the Clouds Operation: Biopoop Cycle The Humus Hurl Would you like me to whip up a mission patch for Operation Full Dump Jacket or maybe draft a press briefing for the Fecal Falcon’s maiden sortie? We can even simulate a malfunction scenario — “Code Brown in Sector 12!” Where to next, Flight Commander?   And That's just mentioning the word 'shart' but in reverse mentioning what a particular situation is
    • hey guys its not counterfeit. i did test its ok needs formatting but how do i do that i need to go in dos and disk Disk Management anyone know step by step please 
    • The first link on the github page gives me a very, very red page warning me about phishing........
  • Recent Achievements

    • First Post
      Fuzz_c earned a badge
      First Post
    • First Post
      TIGOSS earned a badge
      First Post
    • Week One Done
      slackerzz earned a badge
      Week One Done
    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      713
    2. 2
      ATLien_0
      279
    3. 3
      Michael Scrip
      209
    4. 4
      +FloatingFatMan
      201
    5. 5
      Steven P.
      132
  • Tell a friend

    Love Neowin? Tell a friend!