943,695 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 5882
  • C# RSS
Aug 12th, 2009
0

checking for date created/last modified

Expand Post »
i have this code ...

C# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 12th, 2009
0

Re: checking for date created/last modified

You have a lot of dates with files -- created, modified, and accessed.
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Datagrid ComboBox
Next Thread in C# Forum Timeline: Web Browser without webbrowser control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC