且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#代码检查目录中是否存在以aperticular pattern命名的文件?

更新时间:2023-11-26 22:14:22

以下是一些示例: 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.