What is the easiest way to get all files in a folder except for a certain extension? ie. I want to get all files that are not ".pdf"
Printable View
What is the easiest way to get all files in a folder except for a certain extension? ie. I want to get all files that are not ".pdf"
Here's a rough code sample. There are obvious ways to improve the code a great deal, but it at least gives you some direction and a place to start.
Code:Private Function TestFiles() As List(Of String)
Dim d As New System.IO.DirectoryInfo("C:\")
Dim f() As System.IO.FileInfo = d.GetFiles("*.*", IO.SearchOption.TopDirectoryOnly)
Dim tmp As System.IO.FileInfo
Dim FileList As New List(Of String)
For Each tmp In f
If tmp.Extension <> ".pdf" Then FileList.Add(tmp.FullName)
Next
Return FileList
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = "", tmp As List(Of String) = TestFiles()
Dim f As String = ""
For Each f In tmp
s = s & f & vbCrLf
Next
MsgBox(s)
End Sub
Thanks. Thats pretty much how I thought I would have to do it...
There may be a way to filter out file types in the GetFiles method of the DirectoryInfo class; you could probably explore that little further. The method I showed is the one I normally use (or something like it).Quote:
Originally Posted by nbrege
Code:Directory.GetFiles(aPath, "*.pdf")
I'm looking to EXCLUDE .pdf files, not include them...
my bad.
One more way...
vb Code:
Dim PDFFilPth As String PDFFilPth = "C:\Temp\" 'When it finds a PDF file, it adds it to a Combox (cmbPDF): ' Find PDF in Multiple Folders and Lists them Dim rootDi As New DirectoryInfo(PDFFilPth) Dim di As DirectoryInfo For Each di In rootDi.GetDirectories Dim PDFdirs() As String = Directory.GetFiles(PDFFilPth & di.Name) For Each PDFfilname In PDFdirs PDFtestname = System.IO.Path.GetFileName(PDFfilname) 'Check for PDF files and add to Combox 'cmbPDF' Dim FilTest As String = PDFtestname.Remove(0, (Len(PDFtestname) - 3)) If UCase(FilTest) = "PDF" Then cmbPDF.Items.Add (PDFtestname) End If Next Next