【问题标题】:How to sort and delete specific files?如何排序和删除特定文件?
【发布时间】:2013-08-07 13:27:45
【问题描述】:

(我没有意识到长帖子不受欢迎)

我的程序读取的文件目录类似于以下内容:

BkUpSalesReportJan2011(txt).zip
BkUpSalesReportJan2011(pdf).zip
BkUpSalesReportJan2011(doc).zip
BkUpSalesReportFeb2011(txt).zip
BkUpSalesReportMar2011(doc).zip
BkUpSalesReportMar2011(pdf).zip
Goes on for a few hundred more files...

我想根据文件类型(按优先级顺序)仅保存每个报告的一份副本。我想保留 PDF 并删除所有重复项。如果没有 PDF,则保留 DOC,最后保留 TXT。

使用 Visual C# 和 windows 窗体实现排序和删除的最佳方法是什么?

【问题讨论】:

  • TL;DR。你有一个具体的问题,还是你在寻求一般指导?前者是主题,而后者很可能是题外话。
  • 好吧,与其解释为什么会发生大爆炸,不如按照以下指南发布:错误,导致错误的代码,手头的问题。这种解释一切的事情让我想跳过整个事情。
  • 与论坛网站不同,我们不使用“谢谢”、“任何帮助表示赞赏”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
  • 了解您的问题的最佳方法是尝试一下,看看这种方法的优缺点。学习分析并使用它来指导您的重构和重写。
  • 您的问题现在似乎已经简明扼要地陈述了,但您还没有描述您尝试过的内容。

标签: c# winforms file-io


【解决方案1】:

您可以使用 Regex 解析数据的文件名,使用 Linq 获取 Duplicates 或 Distinct 记录。

POCO:

public class FileData
{
    public string Original { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }

    public int Weight { get { return GetWeight(Type); } }

    private static int GetWeight(string option)
    {
        // This will put the files in order by pdf, doc, txt, etc
        switch(option)
        {
            case "pdf":
                return 0;
            case "doc":
                return 1;
            case "txt":
                return 2;
            default:
                return 3;
        }
    }
}

您将需要一个权重函数,因为默认的OrderBy 将按字母顺序工作。这样您就可以指定哪些文件更重要。

代码:

// you can substitute this with Directory.GetFiles
// e.g. var files = Directory.GetFiles("Path/To/Files");
var files = new []
{
    "BkUpSalesReportJan2011(txt).zip",
    "BkUpSalesReportJan2011(pdf).zip",
    "BkUpSalesReportJan2011(doc).zip",
    "BkUpSalesReportFeb2011(txt).zip",
    "BkUpSalesReportMar2011(doc).zip",
    "BkUpSalesReportMar2011(pdf).zip"
};

var pattern = @"(?<FileName>.+)\((?<FileType>\w+)\)\.zip";
// (?<FileName>.+) Match the first part in a named group
// \( Match the first open parenthesis
// (?<FileType>\w+) Match the txt/pdf/doc/whatever in a named group
// \) Match the closing parenthesis
// \.zip Match a period followed by the zip

var matchedFiles = files.Select(f => Regex.Match(f, pattern))
                        .Where(m => m.Success)
                        .Select(f =>
                            new FileData
                                {
                                    Type = f.Groups["FileType"].Value,
                                    Name = f.Groups["FileName"].Value,
                                    Original = f.Value
                                }
                               ).ToList();

// Group all the files by the name e.g. BkUpSalesReportJan2011
// Transform the group into order and take the first record
// Take the original file name to get the originals
var distinct = matchedFiles.GroupBy(f => f.Name)
                           .Select(g => g.OrderBy(f => f.Weight).First())
                           .Select(f => f.Original);

// Group all the files by the name e.g. BkUpSalesReportJan2011
// Transform the group into order and skip the first record
// Since the records are still in a nested IEnumerable we need to flatten it
// Take the original file name to get the duplicates
var duplicates = matchedFiles.GroupBy(f => f.Name)
                             .Select(g => g.OrderBy(f => f.Weight).Skip(1))
                             .SelectMany(g => g)
                             .Select(f => f.Original);

另见:

Directory.GetFiles

【讨论】:

    猜你喜欢
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多