public void PublishDocument(string SourceFilePath, string TargetFolderPath, string TargetFileName, string DocumentHash) { // rename file in the same directory with Hash DocumentFileInfo dfi = this.FileInfoStore.Where(x => x.DocumentHash.Equals(DocumentHash)).FirstOrDefault(); string TargetFilePath = BuildPath(TargetFolderPath, TargetFileName); // if the document exists under the root folder... if (dfi != null) { // if target path is different // otherwise no action is required: file name and hash are identical if (!dfi.FilePath.Equals(TargetFilePath)) { Console.WriteLine("Moving file to \"{0}\".", TargetFilePath); // move the file and rename it DocumentFile.MoveFile(dfi.FilePath, TargetFilePath); } } else { try { Console.WriteLine("Copying file to \"{0}\".", TargetFilePath); // otherwise copy file from source (outside root folder) DocumentFile.CopyFile(SourceFilePath, TargetFilePath); } catch (Exception ex) { Console.WriteLine("ERROR: Unable to copy the file \"{0}\" to \"{1}\". {2}", SourceFilePath, TargetFilePath, ex.Message); } } }
public void ProcessFolder(string FolderPath, bool IgnoreDeleteCheck) { // get current file paths in the folder // exclude temporary files string[] FilePaths = Directory.GetFiles(FolderPath, "*", SearchOption.TopDirectoryOnly); List <DocumentFileInfo> CollectedFileInfos = null; if (!IgnoreDeleteCheck) { //get past file paths from the folder that are not already deleted CollectedFileInfos = AppContext.DocumentFiles .Where(f => f.DeletedFlag.Equals(DocumentFile.FALSE_FLAG_VALUE) && f.FolderPath.Equals(FolderPath, StringComparison.CurrentCultureIgnoreCase)) .Select(x => new DocumentFileInfo { FilePath = x.FilePath, DocumentHash = x.DocumentHash }) .ToList <DocumentFileInfo>(); string[] CollectedFilePaths = CollectedFileInfos.Select(x => x.FilePath).ToArray <string>(); // get files that were previously collected but are no longer in the file list string[] DeletedFilePaths = CollectedFileInfos .Where(x => !FilePaths.Contains(x.FilePath, StringComparer.OrdinalIgnoreCase)) .Select(x => x.FilePath) .ToArray <string>(); if (DeletedFilePaths != null && DeletedFilePaths.Length > 0) { ProcessFileDeletes(DeletedFilePaths); } } // process files foreach (string FilePath in FilePaths) { string DocumentHash = Document.GenerateFileHash(FilePath); // if null then process all file paths as new if (IgnoreDeleteCheck) { ProcessFileInsert(FilePath, DocumentHash); break; } // get the file information match based on file path DocumentFileInfo MatchFileInfo = CollectedFileInfos .Where(x => x.FilePath.Equals(FilePath, StringComparison.CurrentCultureIgnoreCase)) .FirstOrDefault(); // if a file match was not found, assume new insert if (MatchFileInfo == null) { // insert the file ProcessFileInsert(FilePath, DocumentHash); } // compare the hash to determine if update is required // otherwise do nothing else if (!MatchFileInfo.DocumentHash.Equals(DocumentHash)) { // update the file ProcessFileUpdate(FilePath, DocumentHash); } // display message on the console FileCounter += 1; RootFileCounter += 1; if (FileCounter % 100 == 0) { Console.WriteLine("Processed {0} files at {1}.", FileCounter.ToString("#,##0"), DateTime.Now.ToString("s")); } } // final save changes AppContext.SaveChanges(); }