Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DearSir,

I need to last created file without passing fullpath of file name.



Ex.Stanly.doc
Stanly1.doc
stanly2.doc

I can able to pass from the fron end like "C://Project//EMP//Stanly.doc"

My Required Output is:stanly2.doc

Regards
Stanly
Posted
Updated 18-Apr-13 1:33am
v2

This is a little complex, because you want to find the highest numeric value of a group of string based numbers
stanly1.doc
stanly2.doc
stanly10.doc
And the string comparison will say that stanly2.doc has the highest value. You can do it with a little judicial application of LINQ methods and a regex to extract the number:
C#
string[] files = Directory.GetFiles(@"D:\Temp", "stanly*.doc", SearchOption.AllDirectories);
Regex number = new Regex(@"(?<=.*?)\d+(?=\.doc$)");
var highest = files.OrderByDescending(f => int.Parse(number.Match(f).Value)).First();
Console.WriteLine(highest);
 
Share this answer
 
Comments
Thomas Barbare 18-Apr-13 7:55am    
You are right, much better then my solution. +5
You can get the number of files matching the pattern :

int Number=  Directory.GetFiles("path", "pattern").Length;


And then use this number for creating new file ex :

int Number=  Directory.GetFiles("C:\\myfolder", "Stanly*.txt").Length;
File.WriteAllText("C:\\myfolder\\Stanly"+Number+1+".txt","some text");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900