Problem with program that copies files to new folder

Jul 19 2008 3:25 PM
I am writing a program that takes all the files from one folder, EXCEPT the one created today, and copies them to another folder. Everything works ok except copying only the files that were created prior to today. Instead my program copies all of the files over. The part that is highlighted is the part I can't get to work. When the console writes out the creation time it is always of the time the files are being created currently and not when they were created orginally // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it's new directory. // Do not copy if it is current file foreach (FileInfo fi in source.GetFiles()) { if (fi.CreationTime != DateTime.Today) { Console.WriteLine("Creation Time: {0}", fi.CreationTime); Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } } // Delete files from old directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Deleting {0}\{1}", source.FullName, fi.Name); fi.Delete(); } I received some answers on other forums like this one. i.CreationTime contains date and time information, whereas DateTime.Today is a DateTime whose Time part is set to 00:00:00 You need to use fi.CreationTime.Date which also gives a DateTime with zero time. FWIW: DateTime.Today == DateTime.Now.Date BUT I REPLIED TO THIS: hanks for the response. When I try this it only works if I have the operator set to == but I want it to be != because I don't want it to copy the current file. Also, when the console writes out the date it is always todays date. This isn't right because it should be the date of when the file was created. If it is always set to the current date then none of the files will be copied. // Copy each file into it's new directory. // Do not copy if it is current file foreach (FileInfo fi in source.GetFiles()) { if (fi.CreationTime.Date != DateTime.Now.Date) { Console.WriteLine("Creation Time: {0}", fi.CreationTime); Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } } ANY HELP WOULD BE APPRECIATED

Answers (2)