如何获取两个(或三个)文件并删除最旧的文件 [英] How to get two (or three) files and delete the oldest

查看:102
本文介绍了如何获取两个(或三个)文件并删除最旧的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常会使用以下文件将两个文件隐藏在文件名中:请参阅代码段1,因为它接受外卡,因此效果很好。我希望能够单独访问这两个文件中的每一个,这样我就可以选择两个文件中较旧的文件并使用代码片段2将其删除。我无法将这两个文件分开。我认为我的代码只需要一点点调整但我无法理解。



我尝试过的事情:



I get usually two files with the date buried in the filename using: See snippet 1 which works well because it accepts a wild card. I want to have access to each of the two files individually so I can select the older of the two files and delete it, using code like snippet 2. I am having trouble getting the two files separated. I think my code needs just a tiny tweak but I can't figure it out.

What I have tried:

Dim di As DirectoryInfo = New DirectoryInfo("C:\Race\PHRF List\Archive PHRF List\")
        For Each fi In di.GetFiles("*")
            Dim fileNameOnly As String = fi.Name
            Eds = Eds & fileNameOnly & ",  "
        Next
        MsgBox(Eds)

'此代码获取2个文件,用逗号和空格分隔,并在MsgBox中显示它们。我不需要消息框。我用它只是为了看看我是否收到我的文件。



'This code gets the 2 files,separated by a comma and a space and shows them in the MsgBox. I don't need the message box. I used it just to see if I was getting my files.

If file1.CreationTime < file2.CreationTime Then
        File.Delete(file1)
Else If file2.CreationTime < file1.CreationTime Then
        File.Delete(file2)
End If

推荐答案

一点 LINQ 应该让你走上正确的道路。



这是在C#所以你需要做一点翻译,但它会给你文件按创建日期的顺序排列。您可以输入 FirstOrDefault()并获取最旧的文件
A little bit of LINQ should get you on the right path.

This is in C# so you will need to do a little translating, but it will give you the files in order of creation date. You could probably throw in a FirstOrDefault() and just get the oldest file
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime);


这篇关于如何获取两个(或三个)文件并删除最旧的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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