Solved

vb.Net delete files with specific extension

Posted on 2016-09-16
5
88 Views
Last Modified: 2016-09-16
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

0
Comment
Question by:lrbrister
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41801535
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41801543
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

0
 

Author Comment

by:lrbrister
ID: 41801615
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

0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41801767
keep your original code and add the extension to the GetFiles method:
files.AddRange(di.GetFiles("*.txt"))

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 41801811
Bingo

Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question