0

I've researched this across various threads and found viable solutions for one or two parts but I'm having trouble linking the two up. Basically I want to search a directory for file of a certain name , if there is is more than one occurance of this file I then want to choose only the most recent file. e.g if I have three files called DOG I only want the most recent one. I'm using version 1.0 so I don't think Linq is and option. here is the closest match to a solution I've found which I believe will pull the most recent file

var targetDirectory = new DirectoryInfo("G:\SomeFilePath");
string[] files =  System.IO.Directory.GetFiles(Dts.Variables["User::DirectoryPath"].Value.ToString());
string lastFileName=string.Empty;
foreach (string Current_filename in files)
{
    if (string.Compare(Current_filename,lastFileName)>=1)
    {
        lastFileName = Current_filename;          
    }        
}
Dts.Variables["User::LastFileName"].Value = lastFileName;

As the files are all in the same directory my Query is if this code pulls the most recent file , how can I specify what most recent file I want , for example , I have 3 files called dog , 2 called cat etc so how can I take just the most recent Dog file and just the most recent Cat file.

based on answers in this thread I am using this solution https://stackoverflow.com/a/1781668/451518 to return the most recent files , however I know want to seperate these files based on names , e.g I want to search the directory for all DOG , or CAT txt files and then perform the order by most recent function as shown in the link

JUST TO CLARIFY the file names aren't exactly the same i mean DOG2 or DOG3 however its the DOG part that is important so based on the DOG I need to pick the most recent one

In my main I am calling

        if(filename.StartsWith("DOG"))
        {
            GetLatestWritenFileFileInDirectory(directoryInfo);
        }

However I want to edit the GetLatestWritenFileFileInDirectory so that it only takes the files with the name DOG

Community
  • 1
  • 1
user2546071
  • 51
  • 1
  • 10
  • 2
    this can help, order by created datetime, http://stackoverflow.com/questions/4765789/getting-files-by-creation-date-in-net and in combination with http://stackoverflow.com/questions/1179970/c-sharp-find-most-recent-file-in-dir you can get the most recent file(s) – ajt Jul 03 '13 at 10:27
  • 2
    `I'm using version 1.0` Do you mean .Net framework 1.0? – default locale Jul 03 '13 at 10:28
  • `if there is is more than one occurance of this file` It's impossible. Do you want to search in subdirectories? – default locale Jul 03 '13 at 10:31
  • yes using .net framework 1.0 ...the code above should get and order the files correctly however my main problem is making the code A.search for files with the same name B.from these same name files select the most recent – user2546071 Jul 03 '13 at 10:33
  • if its same name or different it will pull all the file names depends on date .... – Glory Raj Jul 03 '13 at 10:34
  • nope dont want to search sub directories , all files are in the same directory so basically I just want to find the matches and then from there pick only the most recent matches – user2546071 Jul 03 '13 at 10:34
  • 1
    @ajt has already linked to a question with non-linq [answer](http://stackoverflow.com/a/1781668/451518). Check it out and if it doesn't work for you, update your question with specific problems – default locale Jul 03 '13 at 10:36
  • the non linq version should work (can't test the code yet) however my main problem is with specifying the search to do as the question says and A.search the directory to find a file of a specified name e.g DOG and if more than one occurance is found to choose the most recent...the non linq code looks as though it will soert the mostrecent first however my problem is searching for the specific files – user2546071 Jul 03 '13 at 11:21
  • In a particular folder, how can there be multiple files with the exact same name? I believe you are not looking for file pattern - you are looking for EXACT name, such as DOG.txt, or just DOG. Or, is it that that you do not care for the file extension? – Anoop Verma Jul 03 '13 at 11:59
  • File extension doesnt matter as they'll all be .txt , the file names will be the similar as in DOG22032013 and DOG23032012 so I need to find similar names and get most recent – user2546071 Jul 03 '13 at 12:07
  • possible duplicate of [C# pick most recent file returning "most recent" based on title rather than write time](http://stackoverflow.com/questions/17484683/c-sharp-pick-most-recent-file-returning-most-recent-based-on-title-rather-than) – billinkc Jul 08 '13 at 02:15

0 Answers0