如何根据文件夹中的命名约定查找文件

本文关键字:命名约定 查找 文件 何根 文件夹 | 更新日期: 2023-09-27 17:50:38

给定一个目录树,根文件夹名为" rootuploaded ",我需要将该树中的文件组合成一些组,使用以下规则:

  • 不同子目录下的文件不能分组
  • 组内文件的扩展名可以相同也可以不同。
  • 每组必须有最少2个文件,最多5个文件
  • 基于6种命名约定(自顶向下优先级)对文件进行分组:

    1. 文件名。FileName_anything ext。FileName_anythingelse ext。ext…
    2. 文件名。FileName-anything ext。FileName-anythingelse ext。ext…
    3. FileName_1。FileName_2 ext。ext,……, FileName_N。ext(可能不连续)
    4. FileName-1。FileName-2 ext。ext,……, FileName-N。ext(可能不连续)
    5. 文件名1。2、文件名称ext,……, FileName N.ext(可能不连续)
    6. FileName1。FileName2 ext。ext,……, FileNameN。ext(可能不连续)

下面是一个简单的程序输入和输出的例子:

输入是一个目录树,如下所示:

    1232年
  • rootuploaded ' samplefolder ' _2342.jpg
  • 1232年
  • rootuploaded ' samplefolder ' _234234_1.jpg
  • 1232年
  • rootuploaded ' samplefolder ' _234234_2.bmp
  • rootuploaded '文件- 5. - txt
  • rootuploaded '文件- 67. - txt
  • rootuploaded ' file-a.txt
  • rootuploaded '中
  • rootuploaded ' file2.txt
  • rootuploaded ' file5.txt
  • rootuploaded ' filea.txt
  • rootuploaded ' file_sample.txt
  • rootuploaded ' file_sample_a.txt
输出:

组1

rootuploaded'samplefolder'1232_234234_1.jpg
rootuploaded'samplefolder'1232_234234_2.bmp

组2

rootuploaded'file1.txt
rootuploaded'file2.txt
rootuploaded'file5.txt

3

rootuploaded'file-5.txt
rootuploaded'file-67.txt

集团4

rootuploaded'file_sample.txt
rootuploaded'file_sample_a.txt

不能分组

rootuploaded'samplefolder'1232_2342.jpg
rootuploaded'file-a.txt
rootuploaded'filea.txt

如何根据文件夹中的命名约定查找文件

使用正则表达式进行分组。Linq的GroupByTake方法可能对其他方法有所帮助。

下面是一些不使用Linq的示例代码。CreateGroups将返回一个列表的列表。每个外部列表匹配一个特定的正则表达式。内部列表中的项是匹配正则表达式的输入。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public class Test
{
    public string[] TestInput = new string[]
    {
        @"rootuploaded'samplefolder'1232_234234_1.jpg",
        @"rootuploaded'samplefolder'1232_234234_2.bmp",
        @"rootuploaded'file-5.txt",
        @"rootuploaded'file-67.txt",
        @"rootuploaded'file-a.txt",
        @"rootuploaded'file1.txt",
        @"rootuploaded'file2.txt",
        @"rootuploaded'file5.txt",
        @"rootuploaded'filea.txt",
        @"rootuploaded'file_sample.txt",
        @"rootuploaded'file_sample_a.txt"
    };
    public string[] RegularExpressions = new string[]
    {
        "[A-Za-z_]+",
        "[A-Za-z-]+",
        "[A-Za-z]+_[0-9]+",
        "[A-Za-z]+-[0-9]+",
        "[A-Za-z]+ [0-9]+",
        "[A-Za-z]+[0-9]+"
    };
    public List<List<string>> CreateGroups(string[] inputs)
    {
        List<List<string>> output = new List<List<string>>(RegularExpressions.Length);
        for (int i = 0; i < RegularExpressions.Length; ++i)
            output.Add(new List<string>());
        foreach (string input in inputs)
        {
            string filename = Path.GetFileNameWithoutExtension(input);
            for (int i = 0; i < RegularExpressions.Length; ++i)
            {
                Match match = Regex.Match(filename, RegularExpressions[i]);
                if (match.Success && match.Length == filename.Length)
                {
                    output[i].Add(input);
                    break;
                }
            }
        }
        return output;
    }
}

显示结果的示例:

Test test = new Test();
var output = test.CreateGroups(test.TestInput);
foreach (List<string> list in output)
{
    string group = "Group:'r'n";
    foreach (string item in list)
        group += "'t" + item + "'r'n";
    Console.WriteLine(group);
}