从文件夹'喜欢'搜索文件 [英] Search file from a Folder 'Like'

查看:74
本文介绍了从文件夹'喜欢'搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆