private void XCount(DirectoryInfo sourceDirectory) { if (DirectoryExclusionsHelper.AllowDirectory(sourceDirectory.FullName)) { foreach (FileInfo fi in sourceDirectory.GetFiles()) { if (MonitoredTypesHelper.AllowFile(fi.FullName, monitoredTypesOnly)) { if (Interrupt.Reason != "Cancel") { totalBytes += fi.Length; SignalUpdateCount(fi.Length); } } } foreach (DirectoryInfo di in sourceDirectory.GetDirectories()) { if (Interrupt.Reason != "Cancel") { XCount(di); } } } //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Count cancelled")); } }
/// <summary> /// Append directory level information into the specified DataTable. /// </summary> /// <param name="qualifier">Top level directory.</param> /// <param name="dirs">Work in progress collection of directories.</param> /// <param name="estimate">Number of directory entries found. Used for future estimating.</param> public void DirLevel(string qualifier, string directory, ref List <string> dirs, ref long estimate) { string spec; string path; string name; path = directory; if (directory.Length > qualifier.Length) { directory = directory.Substring(qualifier.Length); } else { directory = string.Empty; } try { DirectoryInfo directoryInfo = new DirectoryInfo(path); directoryInfo.Refresh(); if (DirectoryExclusionsHelper.AllowDirectory(path)) { DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories(); for (int row = 0; row < directoryInfoArray.Length; row++) { try { name = directoryInfoArray[row].Name; spec = path + Path.DirectorySeparatorChar.ToString() + name; if (DirectoryExclusionsHelper.AllowDirectory(spec)) { if (directoryInfoArray[row].Exists) { dirs.Add(spec); DirectoryEntry entry = new DirectoryEntry(); entry.StdHlq = qualifier.Trim(); entry.StdDir = directory.Trim(); entry.StdFile = name.Trim(); entry.StdSize = 0; entry.StdDate = System.DateTime.Parse("01/01/2000"); entry.SourceDate = entry.StdDate; entry.TargetDate = entry.StdDate; entry.SourceFile = entry.StdFile; entry.TargetFile = entry.StdFile; entry.StdType = "dir"; entry.CtlComparison = string.Empty; directoryListing.Add(entry); estimate += entry.StdSize; SignalUpdateProgress("DirList", entry.StdSize); } } } catch (OleDbException oExceptionA) { System.Diagnostics.Debug.WriteLine(oExceptionA.Errors[0].Message); } if (Interrupt.Reason == "Cancel") { break; } } FileInfo[] fileInfoArray = directoryInfo.GetFiles(); for (int row = 0; row < fileInfoArray.Length; row++) { try { name = fileInfoArray[row].Name; spec = path + Path.DirectorySeparatorChar.ToString() + name; if (fileInfoArray[row].Exists) { string ext = Path.GetExtension(spec); if (ext.StartsWith(".")) { if (ext.Length > 1) { ext = ext.Substring(1); } else { ext = string.Empty; } } if (MonitoredTypesHelper.AllowFile(spec, monitoredTypesOnly)) { DirectoryEntry entry = new DirectoryEntry(); entry.StdHlq = qualifier.Trim(); entry.StdDir = directory.Trim(); entry.StdFile = name.Trim(); entry.StdSize = fileInfoArray[row].Length; entry.StdDate = fileInfoArray[row].LastWriteTime; entry.SourceDate = entry.StdDate; entry.TargetDate = entry.StdDate; entry.SourceFile = entry.StdFile; entry.TargetFile = entry.StdFile; entry.StdType = ext; entry.CtlComparison = string.Empty; directoryListing.Add(entry); estimate += entry.StdSize; SignalUpdateProgress("DirList", entry.StdSize); } } } catch (OleDbException exceptionB) { System.Diagnostics.Debug.WriteLine(exceptionB.Errors[0].Message); } if (Interrupt.Reason == "Cancel") { break; } } } } catch (DirectoryNotFoundException oExceptionNF) { System.Diagnostics.Debug.WriteLine(oExceptionNF.Message); } finally { } }
private void XDelete(DirectoryInfo sourceDirectory, UserSetting.CopyRuleEnum copyRule) { if (DirectoryExclusionsHelper.AllowDirectory(sourceDirectory.FullName)) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Delete From : ""{0}""", sourceDirectory.ToString())); foreach (DirectoryInfo di in sourceDirectory.GetDirectories()) { if (Interrupt.Reason != "Cancel") { XDelete(di, copyRule); } } foreach (FileInfo fi in sourceDirectory.GetFiles()) { if (MonitoredTypesHelper.AllowFile(fi.FullName, monitoredTypesOnly)) { if (fi.Exists) { long fileLength = fi.Length; if (Interrupt.Reason != "Cancel") { try { fi.IsReadOnly = false; fi.Delete(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Deleted", fi.Name)); } catch (Exception ex) { Administrator.Tracer.WriteTimedMsg("E", String.Format(@"Error while deleting : ""{0}""", fi.Name)); Administrator.Tracer.WriteTimedMsg("E", ex.Message); } } SignalUpdateDelete(fileLength); } } } if (Directory.Exists(sourceDirectory.FullName)) { if (Interrupt.Reason != "Cancel") { try { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Removing : ""{0}""", sourceDirectory.FullName)); sourceDirectory.Delete(); } catch (Exception ex) { Administrator.Tracer.WriteTimedMsg("E", String.Format(@"Error while removing : ""{0}""", sourceDirectory.FullName)); Administrator.Tracer.WriteTimedMsg("E", ex.Message); } } } } //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Delete cancelled")); return; } }
private void XCopy(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, UserSetting.CopyRuleEnum copyRule) { if (DirectoryExclusionsHelper.AllowDirectory(sourceDirectory.FullName)) { if (!Directory.Exists(targetDirectory.FullName)) { Directory.CreateDirectory(targetDirectory.FullName); } Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy From : ""{0}""", sourceDirectory.ToString())); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy To : ""{0}""", targetDirectory.ToString())); foreach (FileInfo fi in sourceDirectory.GetFiles()) { if (MonitoredTypesHelper.AllowFile(fi.FullName, monitoredTypesOnly)) { if (Interrupt.Reason != "Cancel") { try { string targetFile = Path.Combine(targetDirectory.ToString(), fi.Name); FileInfo targetFileInfo = new FileInfo(targetFile); bool doCopy = false; if (targetFileInfo.Exists) { if (copyRule == UserSetting.CopyRuleEnum.SkipMatches) { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped", fi.Name)); } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceMatches) { doCopy = true; } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceOnlyWithNewer) { if (CompareDateTimeStamps(fi.LastWriteTime, targetFileInfo.LastWriteTime) > 0) { doCopy = true; } else { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped - Source file is not newer", fi.Name)); } } else if (copyRule == UserSetting.CopyRuleEnum.ReplacePreservingNewest) { if (CompareDateTimeStamps(fi.LastWriteTime, targetFileInfo.LastWriteTime) >= 0) { doCopy = true; } else { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped - Source file is older", fi.Name)); } } } else { doCopy = true; } if (doCopy) { try { if (targetFileInfo.Exists) { targetFileInfo.IsReadOnly = false; } fi.CopyTo(Path.Combine(targetDirectory.ToString(), fi.Name), true); targetFileInfo = new FileInfo(targetFile); if (targetFileInfo.Exists) { targetFileInfo.IsReadOnly = false; } Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Copied", fi.Name)); } catch (Exception ex) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Error : {1}{2}", fi.Name, Environment.NewLine, ex.Message)); Administrator.Tracer.WriteLine(); } } SignalUpdateCopy(fi.Length); } catch (Exception ex2) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Target File (Name too long) Error : {1}{2}", fi.Name, Environment.NewLine, ex2.Message)); Administrator.Tracer.WriteLine(); } } } } foreach (DirectoryInfo di in sourceDirectory.GetDirectories()) { if (Interrupt.Reason != "Cancel") { try { if (DirectoryExclusionsHelper.AllowDirectory(di.FullName)) { DirectoryInfo targetDirectoryInfo = targetDirectory.CreateSubdirectory(di.Name); XCopy(di, targetDirectoryInfo, copyRule); } } catch (Exception ex) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Target Directory (Name too long) Error : {1}{2}", di.Name, Environment.NewLine, ex.Message)); Administrator.Tracer.WriteLine(); } } } } //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy cancelled")); return; } }