/// <summary> /// Get a collection of files that will be copied /// </summary> /// <returns>FileInfo Array</returns> public SFile[] GetFilesToCopy() { SFile[] filesToCopy = new SFile[0]; if (!isBackUpRepValid || !isMasterRepValid) { return(filesToCopy); } //Listing files contained in master repository FileInfo[] masterRepFiles = diMasterRep.GetFiles("*", SearchOption.AllDirectories); List <SFile> masterRepList = (from fn in masterRepFiles select new SFile() { FullName = fn.FullName, FullRelativeName = fn.FullName.Replace(diMasterRep.FullName, ""), DirectoryName = fn.DirectoryName, LastWriteTime = fn.LastWriteTime, Size = fn.Length }).ToList(); //Listing files contained in backup repository FileInfo[] backUpRepFiles = diBackUpRep.GetFiles("*", SearchOption.AllDirectories); List <SFile> backUpRepList = (from fn in backUpRepFiles select new SFile() { FullName = fn.FullName, FullRelativeName = fn.FullName.Replace(diBackUpRep.FullName, ""), DirectoryName = fn.DirectoryName, LastWriteTime = fn.LastWriteTime, Size = fn.Length }).ToList(); /*Listing files that will be copied: * First Condition - Files that are contained in master repository but not in backup repository * OR * Second condition - Files that are in both directories but files in master repository that has a bigger last write time */ filesToCopy = masterRepList.Where( // First condition f => !backUpRepList.Any(bf => bf.FullRelativeName == f.FullRelativeName) || //Second condition ( backUpRepList.Any( bf => bf.FullRelativeName == f.FullRelativeName && f.LastWriteTime > bf.LastWriteTime ) ) ).ToArray(); return(filesToCopy); }
/// <summary> /// Get a collection of files that will be deleted /// </summary> /// <returns>FileInfo Array</returns> public SFile[] GetFilesToDelete() { SFile[] filesToDelete = new SFile[0]; if (!isBackUpRepValid || !isMasterRepValid) { return(filesToDelete); } //Listing files contained in master repository FileInfo[] masterRepFiles = diMasterRep.GetFiles("*", SearchOption.AllDirectories); List <SFile> masterRepList = (from fn in masterRepFiles select new SFile() { FullName = fn.FullName, FullRelativeName = fn.FullName.Replace(diMasterRep.FullName, ""), DirectoryName = fn.DirectoryName, LastWriteTime = fn.LastWriteTime, Size = fn.Length }).ToList(); //Listing files contained in backup repository FileInfo[] backUpRepFiles = diBackUpRep.GetFiles("*", SearchOption.AllDirectories); List <SFile> backUpRepList = (from fn in backUpRepFiles select new SFile() { FullName = fn.FullName, FullRelativeName = fn.FullName.Replace(diBackUpRep.FullName, ""), DirectoryName = fn.DirectoryName, LastWriteTime = fn.LastWriteTime, Size = fn.Length }).ToList(); /*Listing files that will be deleted: * Condition - Files that are contained in backup repository but not in master repository */ filesToDelete = backUpRepList.Where( // First condition bf => !masterRepList.Any(f => f.FullRelativeName == bf.FullRelativeName) ).ToArray(); return(filesToDelete); }