ihorko
ihorko

Reputation: 6945

Directory.GetFiles get today's files only

There is nice function in .NET Directory.GetFiles, it's simple to use it when I need to get all files from directory.

Directory.GetFiles("c:\\Files")

But how (what pattern) can I use to get only files that created time have today if there are a lot of files with different created time?

Thanks!

Upvotes: 17

Views: 34337

Answers (7)

Access Denied
Access Denied

Reputation: 896

var directory = new DirectoryInfo(Path.GetDirectoryName(@"--DIR Path--"));
DateTime from_date = DateTime.Now.AddDays(-5);
DateTime to_date = DateTime.Now.AddDays(5);

//For Today 
var filesLst = directory.GetFiles().AsEnumerable()
              .Where(file.CreationTime.Date == DateTime.Now.Date ).ToArray(); 

//For date range + specific file extension 
var filesLst = directory.GetFiles().AsEnumerable()
              .Where(file => file.CreationTime.Date >= from_date.Date && file.CreationTime.Date <= to_date.Date && file.Extension == ".txt").ToArray(); 

//To get ReadOnly files from directory  
var filesLst = directory.GetFiles().AsEnumerable()
              .Where(file => file.IsReadOnly == true).ToArray(); 

//To get files based on it's size
int fileSizeInKB = 100; 
var filesLst = directory.GetFiles().AsEnumerable()
              .Where(file => (file.Length)/1024 > fileSizeInKB).ToArray(); 

Upvotes: 0

kmatyaszek
kmatyaszek

Reputation: 19296

Try this:

var todayFiles = Directory.GetFiles("path_to_directory")
                 .Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date);

Upvotes: 29

Nicholas Carey
Nicholas Carey

Reputation: 74277

For performance, especially if the directory search is likely to be large, the use of Directory.EnumerateFiles(), which lazily enumerates over the search path, is preferable to Directory.GetFiles(), which eagerly enumerates over the search path, collecting all matches before filtering any:

DateTime today = DateTime.Now.Date ;
FileInfo[] todaysFiles = new DirectoryInfo(@"c:\foo\bar")
                         .EnumerateFiles()
                         .Select( x => {
                            x.Refresh();
                            return x;
                         })
                         .Where( x => x.CreationTime.Date == today || x.LastWriteTime == today )
                         .ToArray()
                         ;

Note that the the properties of FileSystemInfo and its subtypes can be (and are) cached, so they do not necessarily reflect current reality on the ground. Hence, the call to Refresh() to ensure the data is correct.

Upvotes: 30

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

You should be able to get through this:

var loc = new DirectoryInfo("C:\\");


var fileList = loc.GetFiles().Where(x => x.CreationTime.ToString("dd/MM/yyyy") == currentDate);
foreach (FileInfo fileItem in fileList)
{
    //Process the file
}

Upvotes: 0

Sorceri
Sorceri

Reputation: 8033

You need to get the directoryinfo for the file

public List<String> getTodaysFiles(String folderPath)
{
    List<String> todaysFiles = new List<String>();
    foreach (String file in Directory.GetFiles(folderPath))
    {
        DirectoryInfo di = new DirectoryInfo(file);
        if (di.CreationTime.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            todaysFiles.Add(file);
    }
    return todaysFiles;
}

Upvotes: 3

MethodMan
MethodMan

Reputation: 18843

using System.Linq;

DirectoryInfo info = new DirectoryInfo("");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
    // DO Something...
}

if you wanted to break it down to a specific date you could try this using a filter

var files = from c in directoryInfo.GetFiles() 
            where c.CreationTime >dateFilter
            select c;

Upvotes: 0

MUG4N
MUG4N

Reputation: 19717

You could use this code:

var directory = new DirectoryInfo("C:\\MyDirectory");
var myFile = (from f in directory.GetFiles()
             orderby f.LastWriteTime descending
             select f).First();

// or...
var myFile = directory.GetFiles()
             .OrderByDescending(f => f.LastWriteTime)
             .First();

see here: How to find the most recent file in a directory using .NET, and without looping?

Upvotes: 1

Related Questions