如何在文件名的一部分未知时删除VB.NET中的文件 [英] How to delete a file in VB.NET when part of the file name is unknown

查看:132
本文介绍了如何在文件名的一部分未知时删除VB.NET中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件保存在文件夹中以便存档,但是当我下载新的替换文件时,我想删除旧文件。问题很简单,但文件名的一部分是yyyy-mm-dd格式的日期,例如PHRF2019-02-26List.txt,旧日期将是未知的。我以为我可以使用通配符,例如PHRF * List.txt并使用File.Delete,但File.Delete不会使用通配符。我可以想到很多方法可以做到这一点,但它们都非常复杂。我确信有一个简单的方法可以做到这一点,我敢打赌你们中的一个人知道一个非常简单的方法。



我尝试过的:



File.Delete(C:\Stuff \ MoreStuff\PHRF * List.txt)

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")

推荐答案

使用Directory类获取文件列表,并单独删除它们。

试试这个:

Use the Directory class to get a list of files, and delete them individually.
Try this:
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

(通常情况下,我'使用Path类方法提取路径和文件名,但它不能用于通配符)

(Normally, I'd use the Path class methods to extract path and file name, but it doesn't work with wildcard characters)


我建​​议你把你存档的文件的名称放在.txt中如果您每次归档都要删除它,请首先提交文件

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

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





txtarchive.txt作为我的示例文件应该在你的内部调试文件夹



the txtarchive.txt as my sample file should be inside your debug folder


这篇关于如何在文件名的一部分未知时删除VB.NET中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆