Solved

vb.Net delete files with specific extension

Posted on 2016-09-16
5
46 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 69

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 69

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now