每个月删除每个文件名的最新文件 [英] Delete the latest file of each filename each month

查看:68
本文介绍了每个月删除每个文件名的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



首先让我试着描述我想要做什么。



假设我们在一个文件夹中有几个文件。姓名的名称如下:



报告1 2017-08-01.xls

报告1 2017-08-09.xls

报告1 2017-08-27.xls

报告1 2017-09-01.xls

报告1 2017-09-09.xls

报告1 2017-09-18.xls



报告2 2017-08-01.xls

报告2 2017-08-09.xls

报告2 2017-08-27.xls

报告2 2017-09-01.xls

报告2 2017-09-09.xls

报告2 2017-09-22.xls



报告3 2017-08-01 blabla.xls

报告3 2017-08-09 blabla.xls

报告3 2017-08-27 blabla.xls

报告3 2017 -09-01 blabla.xls

报告3 2017-09-09 blabla.xls

报告3 2017-09-30 blabla.xls



现在。



我想删除所有文件,期望每个月每份报告的最新文件。



结果就是这样。删除所有文件exxt the Files:



报告1 2017.08.27.xls

报告1 2017-09-18.xls



报告2 2017-08-27.xls

报告2 2017-09-22.xls



报告3 2017-08-27 blabla.xls

报告3 2017-09-30 blabla.xls



所以首先我列出了Gridview中的所有文件和所属文件夹。



然后我用正则表达式提取所有日期。到目前为止一直很好。



我的问题是如何只删除每个月的每个文件名的最新版本。有没有人有建议?



这是我的代码。



Dear all,

first of all let me try to describe what i am trying to do.

lets say we have several files in a Folder. The Names are name as follows:

Report 1 2017-08-01.xls
Report 1 2017-08-09.xls
Report 1 2017-08-27.xls
Report 1 2017-09-01.xls
Report 1 2017-09-09.xls
Report 1 2017-09-18.xls

Report 2 2017-08-01.xls
Report 2 2017-08-09.xls
Report 2 2017-08-27.xls
Report 2 2017-09-01.xls
Report 2 2017-09-09.xls
Report 2 2017-09-22.xls

Report 3 2017-08-01 blabla.xls
Report 3 2017-08-09 blabla.xls
Report 3 2017-08-27 blabla.xls
Report 3 2017-09-01 blabla.xls
Report 3 2017-09-09 blabla.xls
Report 3 2017-09-30 blabla.xls

Now.

I want to delete all Files expect the Latest File Of each report of each month.

So the result would be. Delete all Files expext the Files:

Report 1 2017.08.27.xls
Report 1 2017-09-18.xls

Report 2 2017-08-27.xls
Report 2 2017-09-22.xls

Report 3 2017-08-27 blabla.xls
Report 3 2017-09-30 blabla.xls

So first i Listed all files and the belonging Folder in a Gridview.

Then i extracted all Dates with Regex. So far so good.

My Problem is how to delete only the latest of each Filename for each month. does anyone have an suggestion?

Here is my code.

      private void button1_Click(object sender, EventArgs e)
      {
          string fileName= "";



          string[] folders  = Directory.GetDirectories(@"C:\Test\", "*", System.IO.SearchOption.AllDirectories);



              for (int i = 0; i < folders.Length; i++)
              {
                  string temp = "";
                  Console.WriteLine("Pfad " + i + " " + folders.GetValue(i));
                  temp = folders.GetValue(i).ToString();

                  dataGridView1.Rows[i].Cells[0].Value = temp;


if ((Directory.GetFiles(temp, "*", SearchOption.TopDirectoryOnly).Length) == 0)

                  {

                 dataGridView1.Rows[i].Cells[1].Value = "No Files in Path";
                  }

                  else

                  {
                      for (int k = 0; k < (Directory.GetFiles(temp, "*", SearchOption.TopDirectoryOnly).Length); k++)
                      {
                          string tempfile = "";
                          tempfile = (Directory.GetFiles(temp, "*", SearchOption.TopDirectoryOnly).GetValue(k).ToString());
                      fileName = Path.GetFileName(tempfile).ToString();

                    Console.WriteLine(Regex.Match(fileName, @"\d{4}-\d{2}-\d{2}"));


                      dataGridView1.Rows[i + k].Cells[0].Value = temp;
                      dataGridView1.Rows[i + k].Cells[1].Value = Path.GetFileName(tempfile).ToString();
                      dataGridView1.Rows[i+k].Cells[2].Value = Regex.Match(fileName, @"\d{4}-\d{2}-\d{2}"));



                  }
                  }


              }
          }







您的帮助非常高。



非常感谢。



我尝试了什么: < br $>


列出所有文件和日期,但不知道如何只删除最新的。




Your help would be highly appriciated.

Thank you very much.

What I have tried:

To list all Files and the dates but dont know how to delete only the latest.

推荐答案

这个将为您简洁地完成:

This will do it succinctly for you:
var inFiles = new List<string>
{
    "Report 1 2017-08-01.xls",
    "Report 1 2017-08-09.xls",
    "Report 1 2017-08-27.xls",
    "Report 1 2017-09-01.xls",
    "Report 1 2017-09-09.xls",
    "Report 1 2017-09-18.xls",
    "Report 2 2017-08-01.xls",
    "Report 2 2017-08-09.xls",
    "Report 2 2017-08-27.xls",
    "Report 2 2017-09-01.xls",
    "Report 2 2017-09-09.xls",
    "Report 2 2017-09-22.xls",
    "Report 3 2017-08-01 blabla.xls",
    "Report 3 2017-08-09 blabla.xls",
    "Report 3 2017-08-27 blabla.xls",
    "Report 3 2017-09-01 blabla.xls",
    "Report 3 2017-09-09 blabla.xls",
    "Report 3 2017-09-30 blabla.xls"
};

var deleteFiles = inFiles.Select(x => new { date = DateTime.Parse(Regex.Match(x, @"\d{4}-\d{2}-\d{2}").Value), file = x })
                            .Select(x => new { id = x.file.Replace(x.date.ToString("yyyy-MM-dd"), ""), date = x.date, file = x.file })
                            .OrderByDescending(x => x.date)
                            .GroupBy(x => x.id)
                            .SelectMany(x => x.Skip(1).Select(y => y.file));

foreach (var file in deleteFiles)
{
    Console.WriteLine(file);
}



输出:


Outputs:

Report 3 2017-09-09 blabla.xls
Report 3 2017-09-01 blabla.xls
Report 3 2017-08-27 blabla.xls
Report 3 2017-08-09 blabla.xls
Report 3 2017-08-01 blabla.xls
Report 2 2017-09-09.xls
Report 2 2017-09-01.xls
Report 2 2017-08-27.xls
Report 2 2017-08-09.xls
Report 2 2017-08-01.xls
Report 1 2017-09-09.xls
Report 1 2017-09-01.xls
Report 1 2017-08-27.xls
Report 1 2017-08-09.xls
Report 1 2017-08-01.xls





更新

为了保持每个月的最新状态,我们需要将分组扩展到包括年份和月份。这将创建更多我们想要的组:



UPDATE
To keep the most recent of each month, we need to expand the grouping to also include year and month. This will create more groups which is what we want:

var deleteFiles =
    inFiles.Select(x => new
                    {
                        date = DateTime.Parse(Regex.Match(x, @"\d{4}-\d{2}-\d{2}").Value),
                        file = x
                    })
            .Select(x => new
                    {
                        id = x.file.Replace(x.date.ToString("yyyy-MM-dd"), ""),
                        year = x.date.Year,
                        month = x.date.Month,
                        date = x.date,
                        file = x.file
                    })
            .OrderByDescending(x => x.date)
            .GroupBy(x => new { id = x.id, year = x.year, month = x.month })
            .SelectMany(x => x.Skip(1).Select(y => y.file));



现在输出为:


Now the output is:

Report 3 2017-09-09 blabla.xls
Report 3 2017-09-01 blabla.xls
Report 2 2017-09-09.xls
Report 2 2017-09-01.xls
Report 1 2017-09-09.xls
Report 1 2017-09-01.xls
Report 1 2017-08-09.xls
Report 1 2017-08-01.xls
Report 2 2017-08-09.xls
Report 2 2017-08-01.xls
Report 3 2017-08-09 blabla.xls
Report 3 2017-08-01 blabla.xls



这是给你的奖励代码...它会告诉你哪个fil正在保存:


Here is a bonus piece of code for you... It will tell you which files are being kept:

Console.WriteLine("-- Keeping:");
foreach (var file in inFiles.Except(deleteFiles))
{
    Console.WriteLine(file);
}



哪些输出:


Which outputs:

-- Keeping:
Report 1 2017-08-27.xls
Report 1 2017-09-18.xls
Report 2 2017-08-27.xls
Report 2 2017-09-22.xls
Report 3 2017-08-27 blabla.xls
Report 3 2017-09-30 blabla.xls


如果你这样做,你会怎么做你自己呢?

我只会用那些具有相同Reportname,Year和Month的文件构建一个List。

现在将这个List降序排序 - 之后你会得到最新档案。 List-Entry 1或更高版本中的每个FileName都可以删除。

在下个月重复此操作,然后在下一年再重复下一个Reportname ...

如果您的报告名称是唯一的,您可以使用循环来完成所有这些...
How would you do it if you do it by yourself ?
I would build a List only with those files which have the same Reportname, Year and Month.
Now sort this List descending - after that you will get the latest File on Top. Each FileName at the List-Entry 1 or higher could be deleted.
Repeat this with the next Month and after that the next Year and then the next Reportname ...
If your Reportnames are unique you can do all this with Loops ...


这篇关于每个月删除每个文件名的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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