开发者

How to search multiple folders for Files with a certain substring in Filename using c#?

I'm wondering how to modify my below code so that I may determine if a File has been duplicated based on the filename. For instance, a file name could look like this "Smith, #10 FINAL 08-20-2010.dwg" and if the file has been resubmitted it will then look like this "Smith, #10 FINAL 08-20-2010_1.dwg" or "Smith, #10 FINAL 08-20-2010_2.dwg", etc... Notice the _1, _2, etc. I need to be able to determine if that File has a _1,_2,_3, etc and copy 开发者_Go百科only the file with the greatest ending value (assumming it has one). I'm just lost on how to determine this. Many thanks..

//Now lets start searching the entire ARCHIVE folder/subfolders for a DWG that has
//this Well_Name and Actual_Date with FINAL in File Name...

DirectoryInfo myDir = new DirectoryInfo(@"C:\G-M");

//Collect the Final, Approved DWGs only...
var files = myDir.GetFileSystemInfos().Where(f => f.Name.Contains("FINAL") ||      f.Name.Contains(drow["Well_Name"].ToString()) || f.Name.Contains(drow["Actual_Date"].ToString()));




 //Copy to directory...
 foreach (FileInfo file in files)
 {
 //Lets loop through and check to see that the File has greatest value if it contains _1, _2, etc..


 System.IO.File.Copy(file.FullName, @"C:\FINAL" + file, true);  
 }


Use regular expression for that

So:

string pattern = @"_(\d+)\.dwg";
if (System.Text.RegularExpressions.Regex.IsMatch("file name", pattern, RegexOptions.IgnoreCase))
{ }

Edit: An idea how to do it is:

System.Text.RegularExpressions.Regex myFileRegex = new Regex(@"_(\d+)\.dwg", RegexOptions.IgnoreCase);

SortedDictionary<string, int> fileFromNumber= new SortedDictionary<int, string>();

foreach (string filePath in Direcoty.GetFiles(@"you directory path")
{
    Match match = myFileRegex.Match(fileName);
    if (match.Groups.Count > 1)
    {
        fileFromNumber.Add(Convert.ToInt32(match.Groups[1].Value), filePath);
    }
}

string newestFile = fileFromNumber[fileFromNumber.Count - 1];//the last element


This code should provide everything you will need:

void Main()
{
    // this is your base scenario
    IEnumerable<FileInfo> fileInfos = new List<FileInfo>{new FileInfo("a_1"), new FileInfo("c"), new FileInfo("a"), new FileInfo("b"), new FileInfo("b_1000")};

    // now use the new class
    IEnumerable<ComparableFileInfo> cFileInfos = fileInfos.Select(x => new ComparableFileInfo(x));

    var nameGroups = cFileInfos.GroupBy(x => x.BaseName).Select(group => new {group.Key, MaxFile = group.Max()});

    foreach (var g in nameGroups.OrderBy(x => x.Key))
    {
        Console.WriteLine(g.MaxFile.OrigFileInfo.FullName);
    }   
}

class ComparableFileInfo : IComparable{

    public FileInfo OrigFileInfo; // omit getters and setters for brevity
    public int FileVersion; 
    public string BaseName;

    public ComparableFileInfo(FileInfo fileInfo){
        this.OrigFileInfo = fileInfo;
        int splitIdx = fileInfo.Name.LastIndexOf("_");
        this.BaseName = splitIdx < 0 ? fileInfo.Name : fileInfo.Name.Substring(0, splitIdx);
        string version = fileInfo.Name.Substring(splitIdx+1, fileInfo.Name.Length - splitIdx -1);
        int versionCast;
        this.FileVersion = Int32.TryParse(version, out versionCast) ? versionCast : 0;
    }

    public int CompareTo(object obj)
    {
        if (obj is ComparableFileInfo)
        {
            ComparableFileInfo f2 = (ComparableFileInfo)obj;
            return this.FileVersion.CompareTo(f2.FileVersion);
        }
        else
            throw new ArgumentException("Object is not a ComparableFileInfo");
    }   
}

Output is:

C:\Users\xxx\Desktop\a_1

C:\Users\xxx\Desktop\b_1000

C:\Users\xxx\Desktop\c

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜