private FileAnalysis AnalyseFile(string filePath, DirectoryAnalysis dirInfo) { Log.Info($"Folder: {dirInfo.Path}"); Log.Info($"Date: {dirInfo.DateCategory}"); DateTime correctDateCategory; if (this.IsFileDateCategoryCorrect(dirInfo.DateCategory, filePath, out correctDateCategory)) correctDateCategory = dirInfo.DateCategory; return new FileAnalysis(dirInfo, filePath, dirInfo.DateCategory, correctDateCategory); }
public FileAnalysis(DirectoryAnalysis parent, string filePath, DateTime dateCategoryCurrently, DateTime dateCategoryDetected) : this(parent, filePath) { DateCategoryCurrently = dateCategoryCurrently; DateCategoryDetected = dateCategoryDetected; if (DateCategoryDetected.Date != DateCategoryCurrently.Date) { var correctDir = $"{parent.PathToDir}\\{DateCategoryDetected.ToString("yyyy-MM-dd")}\\"; // If destination already has file then preserve with a unique file name CorrectedFilePath = correctDir + Path.GetFileName(filePath); var hasConflict = File.Exists(CorrectedFilePath); // Rename the file with a GUID if there is a naming conflict in the new category folder if (hasConflict) { CorrectedFilePath = $"{correctDir}{Path.GetFileNameWithoutExtension(filePath)}_{Guid.NewGuid()}{Path.GetExtension(filePath)}"; if (File.Exists(CorrectedFilePath)) { CorrectedFilePath = null; // Don't move it if no new file path can be assigned // ERROR!!! //throw new InvalidOperationException($"Unable to resolve file conflict as a file with the same resolution name already exists. This is a development issue, please consider raising an issue. File path: '{newFilePath}'"); } } } }
public FileAnalysis(DirectoryAnalysis parent, string filePath) { Parent = parent; FilePath = filePath; DateCategoryCurrently = DateCategoryDetected = DateTime.MinValue; }
public void AddDirectory(DirectoryAnalysis dirInfo) { _directories.Add(dirInfo); }
private bool ShouldAnalyseDirectory(DirectoryAnalysis dirInfo) { // Skip hidden and system directories that might be embedded if (dirInfo.Name.StartsWith(".")) return false; // If we can't parse a date from the directory name then cancel if (dirInfo.DateCategory == DateTime.MinValue) return false; if (!dirInfo.Name.StartsWith("2015-")) return false; // All is OK, parse the directory return true; }
private AnalysisResults RunAnalysis(string libraryPath, CancellationToken cancellationToken, AnalysisResults preAnalysisResults = null) { var isFullAnalysis = preAnalysisResults != null; int totalNumberOfFiles = 0; if (isFullAnalysis) { totalNumberOfFiles = preAnalysisResults?.Directories.Sum(x => x.Files?.Length) ?? 0; } var results = new AnalysisResults(); try { int currentFileCount = 0; foreach (var directoryPath in Directory.EnumerateDirectories(libraryPath)) { var dirInfo = new DirectoryAnalysis(directoryPath); // If we cannot construct a directory info then skip the directory if (!this.ShouldAnalyseDirectory(dirInfo)) continue; foreach (var filePath in Directory.EnumerateFiles(dirInfo.Path)) { // Check if cancelled and exit if so if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } if (!this.ShouldAnalyseFile(filePath)) continue; // Only create a full file analysis if enabled, otherwise just produce a simple file information if (isFullAnalysis) { // Increment the files processed count ++currentFileCount; // Indicate processing only when doing full analysis after a pre-analysis has been performed OnAnalysisProgress(new AnalyserProgressArgs(dirInfo.Name, filePath, currentFileCount, totalNumberOfFiles)); dirInfo.AddFile(this.AnalyseFile(filePath, dirInfo)); } else { dirInfo.AddFile(new FileAnalysis(dirInfo, filePath)); } } // Save the full directory info for the results results.AddDirectory(dirInfo); break; } return results; } catch (OperationCanceledException cex) { Log.Warn("Operation cancelled.", cex); return results; } catch (Exception e) { Log.Error("Error during analysis, aborted.", e); throw; } }