Solved

vb.Net delete files with specific extension

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 3 30
Cant save 3D 4 21
Regular expression help 2 25
Dot Net 4.6.1 installation fails on W2012 R2 server 14 19
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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