Results 1 to 8 of 8

Thread: Getting all files in a folder except...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    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"

  2. #2
    New Member
    Join Date
    Oct 2008
    Posts
    13

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Getting all files in a folder except...

    Thanks. Thats pretty much how I thought I would have to do it...

  4. #4
    New Member
    Join Date
    Oct 2008
    Posts
    13

    Re: Getting all files in a folder except...

    Quote 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).

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Getting all files in a folder except...

    Code:
    Directory.GetFiles(aPath, "*.pdf")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Getting all files in a folder except...

    I'm looking to EXCLUDE .pdf files, not include them...

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Getting all files in a folder except...

    my bad.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting all files in a folder except...

    One more way...

    vb Code:
    1. Dim PDFFilPth As String
    2. PDFFilPth = "C:\Temp\"
    3.  
    4. 'When it finds a PDF file, it adds it to a Combox (cmbPDF):
    5. ' Find PDF in Multiple Folders and Lists them
    6. Dim rootDi As New DirectoryInfo(PDFFilPth)
    7. Dim di As DirectoryInfo
    8. For Each di In rootDi.GetDirectories
    9.     Dim PDFdirs() As String = Directory.GetFiles(PDFFilPth & di.Name)
    10.     For Each PDFfilname In PDFdirs
    11.         PDFtestname = System.IO.Path.GetFileName(PDFfilname)
    12.         'Check for PDF files and add to Combox 'cmbPDF'
    13.         Dim FilTest As String = PDFtestname.Remove(0, (Len(PDFtestname) - 3))
    14.         If UCase(FilTest) = "PDF" Then
    15.             cmbPDF.Items.Add (PDFtestname)
    16.         End If
    17.     Next
    18. 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
  •  



Click Here to Expand Forum to Full Width