Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:19:56+00:00 2026-05-24T06:19:56+00:00

I’m wondering how to modify my below code so that I may determine if

  • 0

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 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);  
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:19:56+00:00Added an answer on May 24, 2026 at 6:19 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.