Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

vb.Net delete files with specific extension

In my code below...
How do I handle and delete ONLY files with a specific extension?

In other words... if there are .BAK and .txt  and .js files...
How can my process below delete only .txt files?
Beginning I would suppose with the following line

files.AddRange(di.GetFiles())


    Private Sub deleteFilesFiles()
        Try
            Dim appPath As String = Application.StartupPath
            Dim strPath As String
            strPath = Trim(File.ReadAllLines(Path.GetFullPath("directorypath.txt")).First()) '"C:\Program FIles\"
            Dim di As New System.IO.DirectoryInfo(strPath)
            Dim files As New List(Of System.IO.FileInfo)
            files.AddRange(di.GetFiles())
            files.Sort(AddressOf SortFilesDescendingByLastWriteTime)
            Dim newestFile As System.IO.FileInfo = files(0)
            Dim dtYear = newestFile.LastWriteTime.DayOfYear
            Dim DeleteDate = DateAdd(DateInterval.Day, -2, newestFile.LastWriteTime)
            For Each file In files
                If file.LastWriteTime < DeleteDate Then
                    file.Delete()
                End If
            Next
        Catch ex As Exception
        End Try
    End Sub

    Private Function SortFilesDescendingByLastWriteTime(ByVal fiA As System.IO.FileInfo, ByVal fiB As System.IO.FileInfo) As Integer
        Return fiB.LastWriteTime.CompareTo(fiA.LastWriteTime)
    End Function

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Try using something like this:

Dim files as String() = System.IO.Directory.GetFiles(path, "*.txt");

This command retrieves a string array of files for a given extension from the requested folder path.
i would replace:
 Dim di As New System.IO.DirectoryInfo(strPath)
Dim files As New List(Of System.IO.FileInfo)
files.AddRange(di.GetFiles())

Open in new window


with:
Dim files As New List(Of System.IO.FileInfo)
files.AddRange(System.IO.Directory.GetFiles(strPath, "*.txt"))

Open in new window

Avatar of Larry Brister

ASKER

Eric
Getting this message on Exception

Message
Unable to cast object of type 'System.String[]' to type 'System.Collections.Generic.IEnumerable`1[System.IO.FileInfo]'.


StackTrace
at FindFIle.Form1.deleteFilesFiles() in C:\Projects\Winforms\FindFIle\FindFIle\Form1.vb:line 21

Line 21 is
files.AddRange(System.IO.Directory.GetFiles(strPath, "*.png"))

This is the full code
    Private Sub deleteFilesFiles()
        Try
            Dim appPath As String = Application.StartupPath
            Dim strPath As String
            strPath = Trim(File.ReadAllLines(Path.GetFullPath("directorypath.txt")).First()) '"C:\Program FIles\"
            Dim files As New List(Of System.IO.FileInfo)
            files.AddRange(System.IO.Directory.GetFiles(strPath, "*.png"))
            files.Sort(AddressOf SortFilesDescendingByLastWriteTime)
            Dim newestFile As System.IO.FileInfo = files(0)
            Dim dtYear = newestFile.LastWriteTime.DayOfYear
            Dim DeleteDate = DateAdd(DateInterval.Day, -2, newestFile.LastWriteTime)
            For Each file In files
                If file.LastWriteTime < DeleteDate Then
                    file.Delete()
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.StackTrace)
        End Try
    End Sub

    Private Function SortFilesDescendingByLastWriteTime(ByVal fiA As System.IO.FileInfo, ByVal fiB As System.IO.FileInfo) As Integer
        Return fiB.LastWriteTime.CompareTo(fiA.LastWriteTime)
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bingo

Thanks