| | |
checking for date created/last modified
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
i have this code ...
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.
C# Syntax (Toggle Plain Text)
namespace ConsoleApplication1 { class watchmove { private static string myString = string.Empty; static void Main(string[] args) { myString = System.Configuration.ConfigurationSettings.AppSettings["DestDir"]; Run(); } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { string[] args = System.Environment.GetCommandLineArgs(); // Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher(); string test1 = System.Configuration.ConfigurationSettings.AppSettings["SourceDir"]; string filetype = System.Configuration.ConfigurationSettings.AppSettings["FileType"]; string filetype2 = System.Configuration.ConfigurationSettings.AppSettings["FileType2"]; watcher.Path = test1;//args[1]; /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = filetype; // Add event handlers. //watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); //watcher.Deleted += new FileSystemEventHandler(OnChanged); //watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching. watcher.EnableRaisingEvents = true; // Wait for the user to quit the program. Console.WriteLine("Press \'q\' to quit the sample."); while (Console.Read() != 'q') ; } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); Console.WriteLine("File: " + e.Name + " " + e.ChangeType); Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath)); string fileName = e.Name; // set the file name to the name of the file found in the folder string sourcePath = Path.GetDirectoryName(e.FullPath); //the source path is the same as the folder being monitored string targetPath = myString; // string that has destination address on it (global variable) // Use Path class to manipulate file and directory paths. string sourceFile = System.IO.Path.Combine(sourcePath, fileName); // creates directories string destFile = System.IO.Path.Combine(targetPath, fileName); // To copy a folder's contents to a new location: // Create a new target folder, if necessary. if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); } // To copy a file to another location and // overwrite the destination file if it already exists. System.IO.File.Copy(sourceFile, destFile, true); if (System.IO.Directory.Exists(sourcePath)) { string[] files = System.IO.Directory.GetFiles(sourcePath); // Copy the files and overwrite destination files if they already exist. foreach (string s in files) { //Use static Path methods to extract only the file name from the path. 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 destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(s, destFile, true); // **change 's' back to sourcefile to copy just one file over! } } else { Console.WriteLine("Source path does not exist!"); } } } }
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.
You have a lot of dates with files -- created, modified, and accessed.
To iterate a directory use this approach:
C# Syntax (Toggle Plain Text)
private void button4_Click(object sender, EventArgs e) { FileInfo fi = new FileInfo(@"C:\picture.bmp"); Console.WriteLine(fi.LastAccessTime); Console.WriteLine(fi.LastWriteTime); Console.WriteLine(fi.CreationTime); if (fi.CreationTime.Date.Equals(DateTime.Today.Date)) //file was created today { //do stuff } }
To iterate a directory use this approach:
C# Syntax (Toggle Plain Text)
private void button4_Click(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo(@"C:\"); FileInfo[] files = di.GetFiles("*.*"); foreach (FileInfo file in files) { FileInfo fi = new FileInfo(@"C:\picture.bmp"); Console.WriteLine(fi.LastAccessTime); Console.WriteLine(fi.LastWriteTime); Console.WriteLine(fi.CreationTime); if (fi.CreationTime.Date.Equals(DateTime.Today.Date)) //file was created today { //do stuff } } }
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: Datagrid ComboBox
- Next Thread: Web Browser without webbrowser control
Views: 1365 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class code color combobox control conversion csharp custom database datagridview dataset datetime degrees development draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ http image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






