C#代码检查目录中是否存在以aperticular pattern命名的文件? [英] C# code to check whether a file named with aperticular pattern exist inside a directory or not?
问题描述
我需要通过C#代码检查目录中是否存在以特定模式命名的文件。
我的目录是
c:\ myfiles\files1
。这个目录有一些以下列模式命名的excel文件。
myfile_17052017
myfile1_18052017.xlsx
myfile1_20052017.xlsx
myfile1_21052017.xlsx
myfile1_22052017.xlsx
myfile1_23052017.xlsx
myfile1_24052017.xlsx
和
C:\ myfiles\files2
< pre> myfile_17052017
myfile2_18052017.xlsx
myfile2_19052017.xlsx
myfile2_20052017.xlsx
myfile2_21052017.xlsx
myfile2_22052017.xlsx
myfile2_24052017.xlsx
和
等..........
这些文件由
中的其他应用程序生成:c:\ myfiles \ files1,c:\ myfiles \ files2,c:\ myfiles \ files3等等。 。 。
目录。
在这里您可以注意到
c中缺少日期19052017的文件:\ myfiles\files1 \
目录和文件23052017c:\ myfiles \ files2 \
directory.I需要检查哪些文件丢失,并将丢失文件的名称及其各自的目录名称存储在上面显示的文件模式的.txt文件中。
我有多个目录,如files1,files2.files3,files4等..在
c:\ myfiles \
目录中如上所述。
所以我需要在
c:\ myfiles \
目录中的所有子目录中运行此检查。
我可以为此要求提供一些C#示例吗?
我尝试了什么:
我是C#的初学者,需要一些我能达到目标的鳕鱼eby的例子。
以下是一些示例: c# - 您可以调用Directory.GetFiles ()有多个过滤器? - 堆栈溢出 [ ^ ]
如果我理解你,你想在文件夹中找到丢失的文件(日期)。
这是一个Linq解决方案:
// 定义初始文件夹和扩展名
string initialdir = @ C:\myfiles跨度>;
string fileExt = * .xlsx 跨度>;
// 从文件名中提取日期所需
System.Globalization.CultureInfo cu = System.Globalization.CultureInfo.InvariantCulture;
// 获取文件和缺少日期
var filesAndDates = Directory.EnumerateFiles(initialdir,fileExt,SearchOption.AllDirectories)
// 按目录名称分组文件
.GroupBy(x = > Path.GetDirectoryName(x))
// 从文件名中选择文件和相应日期
。选择(grp = > new
{
folder = grp.Key,
files = grp.Select(f => Path.GetFileNameWithoutExtension(f))。ToList(),
dates = grp.Select(f => DateTime.ParseExact(Path.GetFileNameWithoutExtension(f).Replace( myfile _, ), ddMMyyyy,cu))。ToList(),
})
// 在文件夹中查找缺少日期
.SelectMany(x = >
// 在当前文件夹中创建最小值和最大值之间的日期范围 - 获取所有日期
// 排除已存在的日期
Enumerable.Range( 0 ,( int )(x.dates.Max() - x.dates.Min())。TotalDays + 1 )
。选择(i = > x.dates.Min()。AddDays(i))
。除外(x .dates)
。选择(y = > new
{
folder = x.folder,
missingDate = y
})
);
foreach ( var fd in filesAndDates)
{
Console.WriteLine( {0} \ t | \t {1},fd.folder,fd.missingDate.ToString( yyyy -MM-dd,cu));
}
结果:
C:\ myfiles\files1 | 2017-05-19
C:\ myfiles\files2 | 2017-05-23
如果您想知道它的工作原理,请阅读上面代码中的评论。
I require a to check whether a file named with a particular pattern exist inside a directory or not through C# code.
Say my directory is
c:\myfiles\files1
. This directory is having some excel files named in the following pattern.
myfile_17052017 myfile1_18052017.xlsx myfile1_20052017.xlsx myfile1_21052017.xlsx myfile1_22052017.xlsx myfile1_23052017.xlsx myfile1_24052017.xlsx
and
C:\myfiles\files2
<pre>myfile_17052017 myfile2_18052017.xlsx myfile2_19052017.xlsx myfile2_20052017.xlsx myfile2_21052017.xlsx myfile2_22052017.xlsx myfile2_24052017.xlsx
and
so on..........
These files are generated by some other application in
c:\myfiles\files1,c:\myfiles\files2,c:\myfiles\files3 and so on. . .
directory.
Here you can notice that file of date
19052017
is missing inside
c:\myfiles\files1\
directory and file of
23052017
is missing inside
c:\myfiles\files2\
directory.I need to check which files are missing and store the names of the missing files with their respective directory name in a .txt file in the file pattern shown above.
I have multiple directories like files1,files2.files3 ,files4 etc.. inside
c:\myfiles\
directory stated as above.
So I need to run this check through out all the sub directories inside
c:\myfiles\
directory.
Can I have some example of C# for this requirement?
What I have tried:
I am a begineer in C# and need some example of cod eby which I can achieve the goal.
Here are some examples: c# - Can you call Directory.GetFiles() with multiple filters? - Stack Overflow[^]
If i understand you well, you want to find missing files (dates) in folders.
Here's a Linq solution:
//define initial folder and extension string initialdir = @"C:\myfiles"; string fileExt = "*.xlsx"; //needed to extract date from file name System.Globalization.CultureInfo cu = System.Globalization.CultureInfo.InvariantCulture; //get files and missing dates var filesAndDates = Directory.EnumerateFiles(initialdir, fileExt, SearchOption.AllDirectories) //group files by directory name .GroupBy(x => Path.GetDirectoryName(x)) //select files and corresponding dates from file name .Select(grp => new { folder = grp.Key, files = grp.Select(f=>Path.GetFileNameWithoutExtension(f)).ToList(), dates = grp.Select(f=>DateTime.ParseExact(Path.GetFileNameWithoutExtension(f).Replace("myfile_", ""), "ddMMyyyy", cu)).ToList(), }) //find missing dates in folders .SelectMany(x => //create date range between min and max in current folder - get all dates //exclude dates which already exist Enumerable.Range(0, (int)(x.dates.Max() - x.dates.Min()).TotalDays + 1) .Select(i => x.dates.Min().AddDays(i)) .Except(x.dates) .Select(y=> new { folder = x.folder, missingDate = y }) ); foreach(var fd in filesAndDates) { Console.WriteLine("{0}\t|\t{1}", fd.folder, fd.missingDate.ToString("yyyy-MM-dd", cu)); }
Result:
C:\myfiles\files1 | 2017-05-19 C:\myfiles\files2 | 2017-05-23
If you would like to know how it works, please read my comments in the code above.
这篇关于C#代码检查目录中是否存在以aperticular pattern命名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!