删除多个文件,同时只保留具有许多不同文件名的最新文件

本文关键字:文件 许多不 最新 文件名 删除 保留 | 更新日期: 2023-09-27 18:29:23

我有一个文件夹,里面有很多不同的文件名,格式为(filename.extension.indexnumber)。我想保留每个文件名的3个最新文件,并删除其余文件。这是我用来编译文件列表的代码。

string dir = Convert.ToString(folderBrowserDialog1.SelectedPath);
DirectoryInfo di = new DirectoryInfo(dir);
int asm = comboBox1.SelectedIndex + 1;
int prt = comboBox2.SelectedIndex + 1;
int drw = comboBox3.SelectedIndex + 1;
var assembliesToBeDeleted = (from a in di.GetFiles("*.asm.*", SearchOption.TopDirectoryOnly)
        orderby a.LastWriteTime descending, a.Name ascending
        select a).ToArray();
foreach (var assembly in assembliesToBeDeleted)
{
    //assembly.Delete();
}

我最后得到的列表是这样的:

scumbox_ga.asm.338
scumbox_ga.asm.337
manway_nozzle.asm.3
inputcheck.asm.452
inputcheck.asm.451
inputcheck.asm.450
inputcheck.asm.449
inputcheck.asm.448
inputcheck.asm.447
cover_w_holes.asm.10
cls_halftank.asm.426
cls_halftank.asm.425
cls_half_final.asm.423
cls_half_final.asm.422
cls_half_final.asm.421
cls_half_final.asm.420
cls_half_final.asm.419
cls_half_final.asm.418
cls_half_final.asm.417
cls_half_final.asm.416
cls_half_final.asm.415
cls_half_final.asm.414
cls_half_final.asm.413
cls_half_final.asm.412
cls_half_final.asm.411
cls_half_final.asm.410
cls_half_final.asm.409

我希望这能澄清问题。

删除多个文件,同时只保留具有许多不同文件名的最新文件

我认为您正在寻找的方法是:

File.Delete(assembly);

定义:此处

所以你的代码最终会看起来像:

string dir = Convert.ToString(folderBrowserDialog1.SelectedPath);
DirectoryInfo di = new DirectoryInfo(dir);
int asm = comboBox1.SelectedIndex + 1;
int prt = comboBox2.SelectedIndex + 1;
int drw = comboBox3.SelectedIndex + 1;
var assembliesToBeDeleted = (from a in di.GetFiles("*.asm.*", SearchOption.TopDirectoryOnly)
                                     orderby a.LastWriteTime descending, a.Name ascending
                                     select a).ToArray();
 foreach (var assembly in assembliesToBeDeleted)
 {
      File.Delete(assembly);
      Console.WriteLine(assembly);
 }

假设备份过程添加的扩展名被填充为三个字符的扩展名,用零填充缺失的数字

因此,拥有一组具有这种模式的文件

"ABCD.001"
"ABCD.002"
"ABCD.003"
"ABCD.004"
"ABCD.005"

如果你想保留文件003004005,那么你可以将代码更改为

DirectoryInfo di = new DirectoryInfo(dir);
// Get the distinct base file names of every file in that directory
// So for a bunch of files 'Name.XX1','Name.XX3','Name.XX3' we
// get an IEnumerable of only the Name part
var files = di.GetFiles("*.*", SearchOption.TopDirectoryOnly)
               .Select(x => Path.GetFileNameWithoutExtension(x.Name)).Distinct();
// Now loop over each base file name
foreach (string file in files)
{
    // Get the files with that base name in descending order but skipping the first 3
    var toDelete = di.GetFiles(file + ".*").OrderByDescending(d => d.Name).Skip(3);
    // Delete if any file is in the IEnumerable returned
    foreach(FileInfo fileToDelete in toDelete)
        fileToDelete.Delete();
}

如果你的扩展名没有填充三个字符,那么你应该恢复使用FileInfo 的LastWriteTime

var toDelete = di.GetFiles(file + ".*").OrderByDescending(d => d.LastWriteTime).Skip(3);

我会创建一个文件名对象来容纳这三部分。

class AssemblyFilename
{
  public string Name { get; set; }
  public string Part { get; set; }
  public int Version { get; set; }
}

创建一个构造函数来解析出部分

public AssemblyFilename(string filename) { /*...*/ }

然后你可以使用LINQ

var fileGroups = assemblyFilenames.GroupBy(af => af.Name);
foreach(var group in fileGroups)
{
  foreach(var filenameToDelete in group.OrderByDescending(g => g.Version).Skip(3))
  {
    // delete
  }
}