从文件夹'喜欢'搜索文件 [英] Search file from a Folder 'Like'
问题描述
DearSir,
我需要上传创建的文件而不传递文件名的完整路径。
Ex.Stanly.doc
Stanly1.doc
stanly2.doc
我可以像C://Project//EMP//Stanly.doc那样从前端传来。
我要求的输出是:stanly2.doc
问候
Stanly
这有点复杂,因为你想找到一组基于字符串的数字的最高数值
stanly1.doc
stanly2.doc
stanly10.doc字符串比较会说stanly2.doc的值最高。您可以通过一些LINQ方法的司法应用程序和正则表达式来提取数字:
string [] files = Directory.GetFiles( @ D:\ Temp, stanly * .doc,SearchOption.AllDirectories);
正则表达式编号= 新正则表达式( @ (小于??= *)\d +(= \.doc
?)跨度>);
var highest = files.OrderByDescending(f = > INT 跨度> .Parse(number.Match(F)。价值))首先();
Console.WriteLine(最高);
您可以获得与该模式匹配的文件数量:
int Number = Directory.GetFiles(path,pattern)。Length;
然后使用这个用于创建新文件的编号ex:
int Number = Directory.GetFiles(C:\\ myfolder,Stanly *。 TXT)长度。
File.WriteAllText(C:\\myfolder \\Stanly+ Number + 1 +。txt,some text);
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
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.docAnd 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:
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);
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");
这篇关于从文件夹'喜欢'搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!