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