如何在 C# 中查找最新版本的文件名

DaB*_*ars 5 c# regex directory file-io

我有一个充满 dwg 文件的文件夹,因此我只需要查找文件的最新版本,或者如果文件没有版本,则将其复制到目录中。例如这里有三个文件:

ABBIE 08-10 #6-09H4 最终版 06-12-2012.dwg
ABBIE 08-10 #6-09H4 最终版 06-12-2012_1.dwg
ABBIE 08-10 #6-09H4 最终版 06-12-2012_2.dwg

请注意,区别在于一个文件有一个_1,另一个文件有一个_2,所以这里最新的文件是_2. 我需要保留最新的文件并将其复制到目录中。有些文件不会有不同的版本,因此可以复制它们。我无法关注文件的创建日期或修改日期,因为在许多情况下它们是相同的,所以我所要做的就是文件名本身。我确信有比我在下面发布的方法更有效的方法来做到这一点。

DirectoryInfo myDir = new DirectoryInfo(@"H:\Temp\Test");
var Files = myDir.GetFiles("*.dwg");

string[] fileList = Directory.GetFiles(@"H:\Temp\Test", "*FINAL*", SearchOption.AllDirectories);

ArrayList list = new ArrayList();
ArrayList WithUnderscores = new ArrayList();
string nameNOunderscores = "";

for (int i = 0; i < fileList.Length; i++)
{
    //Try to get just the filename..
    string filename = fileList[i].Split('.')[0];
    int position = filename.LastIndexOf('\\');
    filename = filename.Substring(position + 1);
    filename = filename.Split('_')[0];

    foreach (FileInfo allfiles in Files)
    {
        var withoutunderscore = allfiles.Name.Split('_')[0];
        withoutunderscore = withoutunderscore.Split('.')[0];
        if (withoutunderscore.Equals(filename))
        {
            nameNOunderscores = filename;
            list.Add(allfiles.Name);
        }
    }

    //If there is a number after the _ then capture it in an ArrayList
    if (list.Count > 0)
    {
        foreach (string nam in list)
        {
            if (nam.Contains("_"))
            {
                //need regex to grab numeric value after _
                var match = new Regex("_(?<number>[0-9]+)").Match(nam);
                if (match.Success)
                {
                    var value = match.Groups["number"].Value;
                    var number = Int32.Parse(value);

                    WithUnderscores.Add(number);
                }
            }
        }

        int removedcount = 0;

        //Whats the max value?
        if (WithUnderscores.Count > 0)
        {
            var maxval = GetMaxValue(WithUnderscores);
            Int32 intmax = Convert.ToInt32(maxval);

            foreach (FileInfo deletefile in Files)
            {
                string shorten = deletefile.Name.Split('.')[0];
                shorten = shorten.Split('_')[0];
                if (shorten == nameNOunderscores && deletefile.Name != nameNOunderscores + "_" + intmax + ".dwg")  
                {
                    //Keep track of count of Files that are no good to us so we can iterate to next set of files
                    removedcount = removedcount + 1;

                }
                else
                {
                    //Copy the "Good" file to a seperate directory
                    File.Copy(@"H:\Temp\Test\" + deletefile.Name, @"H:\Temp\AllFinals\" + deletefile.Name, true); 
                }
            }

            WithUnderscores.Clear();
            list.Clear();
        }

        i = i + removedcount;
    }
    else
    {
        //This File had no versions so it is good to be copied to the "Good" directory
        File.Copy(@"H:\Temp\SH_Plats\" + filename, @"H:\Temp\AllFinals" + filename, true);
        i = i + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dea*_*bit 1

我已经制定了一个基于正则表达式的解决方案,但显然在此期间来晚了。

(?<fileName>[A-Za-z0-9-# ]*)_?(?<version>[0-9]+)?\.dwg

这个正则表达式将识别文件名和版本并将它们分成组,一个非常简单的 foreach 循环来获取字典中的最新文件(因为我很懒),然后您只需在访问之前将文件名重新组合在一起他们。

var fileName = file.Key + "_" + file.Value + ".dwg"

完整代码

var files = new[] {
    "ABBIE 08-10 #6-09H4 FINAL 06-12-2012.dwg",
    "ABBIE 08-10 #6-09H4 FINAL 06-12-2012_1.dwg",
    "ABBIE 08-10 #6-09H4 FINAL 06-12-2012_2.dwg",
    "Second File.dwg",
    "Second File_1.dwg",
    "Third File.dwg"
};

// regex to split fileName from version
var r = new Regex( @"(?<fileName>[A-Za-z0-9-# ]*)_?(?<version>[0-9]+)?\.dwg" );
var latestFiles = new Dictionary<string, int>();

foreach (var f in files)
{
    var parsedFileName = r.Match( f );
    var fileName = parsedFileName.Groups["fileName"].Value; 
    var version = parsedFileName.Groups["version"].Success ? int.Parse( parsedFileName.Groups["version"].Value ) : 0;

    if( latestFiles.ContainsKey( fileName ) && version > latestFiles[fileName] )
    {
        // replace if this file has a newer version
        latestFiles[fileName] = version;
    }
    else
    {
        // add all newly found filenames
        latestFiles.Add( fileName, version );
    }
}

// open all most recent files
foreach (var file in latestFiles)
{
    var fileToCopy = File.Open( file.Key + "_" + file.Value + ".dwg" );
    // ...
}
Run Code Online (Sandbox Code Playgroud)