|
-
Oct 30th, 2008, 08:50 AM
#1
Thread Starter
Frenzied Member
Getting all files in a folder except...
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"
-
Oct 30th, 2008, 09:14 AM
#2
New Member
Re: Getting all files in a folder except...
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
-
Oct 30th, 2008, 09:25 AM
#3
Thread Starter
Frenzied Member
Re: Getting all files in a folder except...
Thanks. Thats pretty much how I thought I would have to do it...
-
Oct 30th, 2008, 09:40 AM
#4
New Member
Re: Getting all files in a folder except...
 Originally Posted by nbrege
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).
-
Oct 30th, 2008, 09:40 AM
#5
Re: Getting all files in a folder except...
Code:
Directory.GetFiles(aPath, "*.pdf")
-
Oct 30th, 2008, 09:56 AM
#6
Thread Starter
Frenzied Member
Re: Getting all files in a folder except...
I'm looking to EXCLUDE .pdf files, not include them...
-
Oct 30th, 2008, 09:59 AM
#7
Re: Getting all files in a folder except...
-
Oct 30th, 2008, 10:25 AM
#8
Re: Getting all files in a folder except...
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|