TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
John Strzempa
NA
3
0
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
Reply
Answers (
2
)
Watching events using WMI and WQL
Hooking Keystrokes