简体   繁体   English

搜索带有时间戳的文件

[英]searching file having timestamp

I have a scenario like this in my application.I want to search for a file name which has a time stamp.I have done like: 我的应用程序中有一个这样的场景。我想搜索一个带有时间戳的文件名。

 if (System.IO.File.Exists("C:\\ScriptLog\\ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH") + ".log") == true)
 {
   //tasks
 }

But this doesn't seem to be working...Can anyone please support on this? 但这似乎不起作用...任何人都可以对此提供支持吗?

What I want is that I should be able to search for files created in that hour. 我想要的是我应该能够搜索在该小时内创建的文件。 Suppose if the present time is 10:43:04, then I should be able to check the files created at 10:00:00 upto now.. 假设当前时间是10:43:04,那么我应该能够检查到现在10:00:00创建的文件。

var files = Directory.GetFiles("C:\\ScriptLog\\", "*.log");
foreach(var file in files)
{
    var fileInfo = new FileInfo(file);
    if(fileInfo.CreationTime >= /*your date here*/)
    {
        //This is the file you are searching for
    }
}

This should work: 这应该工作:

var files = new DirectoryInfo("C:\\ScriptLog")
    .EnumerateFiles("*.log")
    .Where(x => x.CreationTime.Hour >= DateTime.Now.Hour); 

if (files.Any() != null)
{
    // tasks
}

For me you should get the file list of whole directory, and then check the time stamp. 对我来说,您应该获取整个目录的文件列表,然后检查时间戳。 If you want to check only the current hour then the code will look like: 如果您只想检查当前时间,则代码如下所示:

string[] ListFiles = System.IO.Directory.GetFiles("C:\\ScriptLog\\", "*.log*");
string currHour = DateTime.Now.ToString("HH");

foreach (string s in ListFiles)
{
   if (s.EndsWith(currHour + ".log"))
   {
      //tasks
   }
}

j Ĵ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM