checking for date created/last modified

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

checking for date created/last modified

 
0
  #1
Aug 12th, 2009
i have this code ...

  1. namespace ConsoleApplication1
  2. {
  3. class watchmove
  4. {
  5. private static string myString = string.Empty;
  6.  
  7. static void Main(string[] args)
  8. {
  9. myString = System.Configuration.ConfigurationSettings.AppSettings["DestDir"];
  10.  
  11.  
  12. Run();
  13. }
  14.  
  15. [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  16. public static void Run()
  17. {
  18. string[] args = System.Environment.GetCommandLineArgs();
  19.  
  20. // Create a new FileSystemWatcher and set its properties.
  21. FileSystemWatcher watcher = new FileSystemWatcher();
  22.  
  23. string test1 = System.Configuration.ConfigurationSettings.AppSettings["SourceDir"];
  24. string filetype = System.Configuration.ConfigurationSettings.AppSettings["FileType"];
  25. string filetype2 = System.Configuration.ConfigurationSettings.AppSettings["FileType2"];
  26.  
  27. watcher.Path = test1;//args[1];
  28. /* Watch for changes in LastAccess and LastWrite times, and
  29.   the renaming of files or directories. */
  30. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
  31. | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  32. // Only watch text files.
  33. watcher.Filter = filetype;
  34. // Add event handlers.
  35. //watcher.Changed += new FileSystemEventHandler(OnChanged);
  36. watcher.Created += new FileSystemEventHandler(OnChanged);
  37. //watcher.Deleted += new FileSystemEventHandler(OnChanged);
  38. //watcher.Renamed += new RenamedEventHandler(OnRenamed);
  39.  
  40. // Begin watching.
  41. watcher.EnableRaisingEvents = true;
  42.  
  43. // Wait for the user to quit the program.
  44. Console.WriteLine("Press \'q\' to quit the sample.");
  45. while (Console.Read() != 'q') ;
  46. }
  47.  
  48. // Define the event handlers.
  49. private static void OnChanged(object source, FileSystemEventArgs e)
  50. {
  51. // Specify what is done when a file is changed, created, or deleted.
  52. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
  53. Console.WriteLine("File: " + e.Name + " " + e.ChangeType);
  54. Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
  55.  
  56. string fileName = e.Name; // set the file name to the name of the file found in the folder
  57. string sourcePath = Path.GetDirectoryName(e.FullPath); //the source path is the same as the folder being monitored
  58. string targetPath = myString; // string that has destination address on it (global variable)
  59.  
  60. // Use Path class to manipulate file and directory paths.
  61. string sourceFile = System.IO.Path.Combine(sourcePath, fileName); // creates directories
  62. string destFile = System.IO.Path.Combine(targetPath, fileName);
  63.  
  64. // To copy a folder's contents to a new location:
  65. // Create a new target folder, if necessary.
  66. if (!System.IO.Directory.Exists(targetPath))
  67. {
  68. System.IO.Directory.CreateDirectory(targetPath);
  69. }
  70.  
  71. // To copy a file to another location and
  72. // overwrite the destination file if it already exists.
  73. System.IO.File.Copy(sourceFile, destFile, true);
  74.  
  75. if (System.IO.Directory.Exists(sourcePath))
  76. {
  77. string[] files = System.IO.Directory.GetFiles(sourcePath);
  78.  
  79. // Copy the files and overwrite destination files if they already exist.
  80. foreach (string s in files)
  81. {
  82. //Use static Path methods to extract only the file name from the path.
  83. fileName = System.IO.Path.GetFileName(s); //**change this back to e.name if its only one file you want to copy and not all of them
  84.  
  85. destFile = System.IO.Path.Combine(targetPath, fileName);
  86. System.IO.File.Copy(s, destFile, true); // **change 's' back to sourcefile to copy just one file over!
  87. }
  88. }
  89. else
  90. {
  91. Console.WriteLine("Source path does not exist!");
  92. }
  93.  
  94.  
  95. }
  96.  
  97.  
  98. }
  99. }

it works great for copying the files in a folder once the a certain file comes in, but i need it to only copy files that come in on the date on that day. eg only copy files with todays date.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,230
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 576
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: checking for date created/last modified

 
0
  #2
Aug 12th, 2009
You have a lot of dates with files -- created, modified, and accessed.
  1. private void button4_Click(object sender, EventArgs e)
  2. {
  3. FileInfo fi = new FileInfo(@"C:\picture.bmp");
  4. Console.WriteLine(fi.LastAccessTime);
  5. Console.WriteLine(fi.LastWriteTime);
  6. Console.WriteLine(fi.CreationTime);
  7. if (fi.CreationTime.Date.Equals(DateTime.Today.Date)) //file was created today
  8. {
  9. //do stuff
  10. }
  11. }

To iterate a directory use this approach:
  1. private void button4_Click(object sender, EventArgs e)
  2. {
  3. DirectoryInfo di = new DirectoryInfo(@"C:\");
  4. FileInfo[] files = di.GetFiles("*.*");
  5. foreach (FileInfo file in files)
  6. {
  7. FileInfo fi = new FileInfo(@"C:\picture.bmp");
  8. Console.WriteLine(fi.LastAccessTime);
  9. Console.WriteLine(fi.LastWriteTime);
  10. Console.WriteLine(fi.CreationTime);
  11. if (fi.CreationTime.Date.Equals(DateTime.Today.Date)) //file was created today
  12. {
  13. //do stuff
  14. }
  15. }
  16. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC