Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.Net delete files with specific extension

Posted on 2016-09-16
5
Medium Priority
?
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

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 2000 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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