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

时间:2016-01-06 15:56:30

标签: c# file-management

我有一个文件夹,其中有许多不同的文件名,格式为(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

我希望这能澄清这个问题。

3 个答案:

答案 0 :(得分:0)

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

File.Delete(assembly);

定义:here

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

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);
 }

答案 1 :(得分:0)

假设您的备份程序添加的扩展名被填充为三个字符的扩展名,填充缺少的数字为零

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

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

您要保留文件003,004,005然后您可以将代码更改为

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

答案 2 :(得分:0)

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

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