Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I would like to develop a application using vb.net to move the files from one folder to another where created date of the files between 2 dates.

for example If i wasnt to transfer 01/09/2016 to 31/09/2016. Something like that.

Is that possible by date range?

Thanks,
Darvin

What I have tried:

Dim sourcepath As String = TextBox1.Text
Dim DestPath As String = TextBox2.Text
Dim datefrom As String = TextBox3.Text
Dim dateto As String = TextBox4.Text



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.IO.Directory.CreateDirectory(DestPath)
End Sub
End Class
Posted
Updated 16-Sep-16 3:35am
Comments
Karthik_Mahalingam 16-Sep-16 9:23am    
You have just declared the variables.
[no name] 16-Sep-16 9:34am    
Look at the File class and see if there might be some method that might help you. Other than that you haven't actually tried anything only asked if it were possible. Yes it is possible. There is your answer.

Yes, it's possible - you can use DirectoryInfo:
C#
DateTime startDate = DateTime.Now.AddMonths(-1);
DateTime endDate = DateTime.Now;
DirectoryInfo di = new DirectoryInfo(@"D:\Temp");
FileInfo[] files = di.GetFiles();
List<string> toMove = files.Where(fi => fi.CreationTime >= startDate && fi.CreationTime <= endDate)
                           .Select(fi => fi.FullName)
                           .ToList();
foreach (string file in toMove)
    {
    // Move the file
    ...
    }
 
Share this answer
 
sure - what you need is something like

DirectoryInfo DirInfo = new DirectoryInfo(sourcepath);

Dim datePattern As String = "dd/MM/yyyy"
Dim dateFrom  As Date = Date.Parse(datefrom, datePattern, Nothing)
Dim dateTo As Date = Date.Parse(dateTo, datePattern, Nothing)

// LINQ query for files between dateFrom & dateTo
var files = from file in DirInfo.EnumerateFiles()
where file.CreationTimeUtc >= dateFrom &
file.CreationTimeUtc <= dateTo                       
select file;


To get a list of the matching files in the source path - You'll have to decide wether you want to compare on modifed date, created date etc, and there's no error handling in the above .. you can then

foreach (var file in files)
{
// do something with 'file' like copy it to DestPath 
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900