How to get list of files which were updated today? .NET
I have thousands of files in a FTP server which I can connect using windows explorer. Day by day, during evening, I have to find the files which were changed (uploaded) today and take their name. I'd like to develop a visual basic batch application that, just running the EXE with a parameter (date), take a list of the names of those files.
Recently I found this code here:
source = "\\ftp_my\Upload\" searchfor = "*" Dim dirs As String() = Directory.GetFiles(source, searchfor) For Each file In dirs fileinfo = New FileInfo(file) If (fileinfo.LastWriteTime >= DateAdd(DateInterval.Day, -1, Date.Today)) Then 'execute my actions End If Next
But it takes an increditable time to list all files and evaluate their properties. On the other hand , if I just make a "dir", export to excel, split the text and evaluate manually the date / files I want I finish that study earlier than the VB!
Therefore there should be a command that list the files ordered by date DESC during a resonable period, is there?
A couple of thing I noticed that can slow your iteration down.
You're casting each file name to a fileinfo object instead of using a collection of fileinfo objects.
You're calculating the target date to check for, for each file, instead of calculating it once.
Here's a method to do what you want using LINQ that will find 1000 files(worst case scenario), on a mapped network drive, in less than 5 seconds:
Dim source = New DirectoryInfo("\\ftp_my\Upload\") Dim searchfor = "*" Dim TargetDate = DateAdd(DateInterval.Day, -1, Date.Today) Dim dirs = (From f As FileInfo In source.GetFiles(searchfor) Where f.CreationTime >= TargetDate Select f).ToList
dirs will contain all the fileinfo objects for each file you want to target.
If iterating through dirs and completing your actions on each file takes a long time then this will tell you where the time is being spent, and you can start looking at that code and start optimizing it to cut down the time.