Click here to Skip to main content
15,891,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file that I save in a folder for archive purposes but when I download a new replacement file I want to delete the old file. The problem would be easy but part of the file name is the date in yyyy-mm-dd format, e.g., PHRF2019-02-26List.txt and the old date will be unknown. I thought I could use wild cards, e.g., PHRF*List.txt and use File.Delete but File.Delete will not take wild cards. I can think of a number of ways to do this but they are all very complex. I am sure there is a simple way to do this and I'll bet one of you out there knows a very simple way.

What I have tried:

File.Delete("C:\Stuff\MoreStuff\PHRF*List.txt")
Posted
Updated 28-Jun-22 18:03pm

Use the Directory class to get a list of files, and delete them individually.
Try this:
VB
Private Shared Sub DeleteMatching(ByVal matchPath As String)
    Dim lastSlash As Integer = matchPath.LastIndexOf("\"c)
    Dim path As String = matchPath.Substring(0, lastSlash)
    Dim match As String = matchPath.Substring(lastSlash + 1)
    Dim files As String() = Directory.GetFiles(path, match)

    For Each file As String In files
        File.Delete(file)
    Next
End Sub
(Normally, I'd use the Path class methods to extract path and file name, but it doesn't work with wildcard characters)
 
Share this answer
 
v2
Comments
Member 10628309 4-Mar-19 18:45pm    
Thank you for your quick response. I didn't respond sooner because so far I am not able to utilize your suggested code. But, I am not throwing it away either
I suggest you put the name of the file you archived in a .txt file first if your gonna delete it every time you archive
VB
sub save_name()
        Dim filenewname = "PHRF2019-02-26List.txt"
        System.IO.File.WriteAllText(Application.StartupPath & "\txtarchive.txt", "") 'clean content file
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath &          "\txtarchive.txt", True)
        file.Write(filenewname) 'write new filename content
        file.Close()
end sub


so you can easily delete them afterwards:

VB
sub deletearchived()
        Dim stringss As String
        Dim reader As New StreamReader(Application.StartupPath & "\txtarchive.txt")
        stringss = reader.ReadToEnd
        MsgBox(stringss) 'test content of the txt file
        reader.Close()
        File.Delete("C:\Stuff\MoreStuff\" & stringss) 'delete the file
end sub


the txtarchive.txt as my sample file should be inside your debug folder
 
Share this answer
 
Comments
Member 10628309 4-Mar-19 18:49pm    
Thank you for your quick response. I have not been able to use your code as yet but it may come in handy when I resolve a related issue.
In VBNET2022
Search inside the Directory, if it matched then Delete it.

VB
Dim thePath As String = Server.MapPath("\archives\documents")
Dim theFolder As String() = Directory.GetFiles(thePath)
Dim theSearch As String = "PHRF" ' or else

For Each theFile As String In theFolder
  If theFile.IndexOf(theSearch) > 0 Then
    File.Delete(theFile)
  End If
Next
 
Share this answer
 
Comments
Dave Kreskowiak 29-Jun-22 1:07am    
I doubt the OP is still looking for a solution three years later.
Ralf Meier 29-Jun-22 3:28am    
also it's not complete that what the OP has asked for.
In my opinion the 1st Solution from Griff was the best ...
Member 10628309 29-Jun-22 14:02pm    
Member 15690654 I have not tried it yet but I like your solution. I have been using a workaround but even 3 years later I appreciate you diligence. I rationalize your solution like using Regex to find a match for the path and filename and if found delete it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900