Larry Brister
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 ())
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
i would replace:
with:
Dim di As New System.IO.DirectoryInfo(strPath)
Dim files As New List(Of System.IO.FileInfo)
files.AddRange(di.GetFiles())
with:
Dim files As New List(Of System.IO.FileInfo)
files.AddRange(System.IO.Directory.GetFiles(strPath, "*.txt"))
ASKER
Eric
Getting this message on Exception
Message
Unable to cast object of type 'System.String[]' to type 'System.Collections.Generi c.IEnumera ble`1[Syst em.IO.File Info]'.
StackTrace
at FindFIle.Form1.deleteFiles Files() in C:\Projects\Winforms\FindF Ile\FindFI le\Form1.v b:line 21
Line 21 is
files.AddRange(System.IO.D irectory.G etFiles(st rPath, "*.png"))
This is the full code
Getting this message on Exception
Message
Unable to cast object of type 'System.String[]' to type 'System.Collections.Generi
StackTrace
at FindFIle.Form1.deleteFiles
Line 21 is
files.AddRange(System.IO.D
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bingo
Thanks
Thanks
Dim files as String() = System.IO.Directory.GetFil
This command retrieves a string array of files for a given extension from the requested folder path.