public bool TryFindBaseFileForConflictFile(string filePath, out ParsedConflictFileInfo parsedConflictFileInfo) { var directory = Path.GetDirectoryName(filePath); var fileName = Path.GetFileName(filePath); var parsed = conflictRegex.Match(fileName); if (!parsed.Success) { parsedConflictFileInfo = default(ParsedConflictFileInfo); return false; } var prefix = parsed.Groups["prefix"].Value; var year = Int32.Parse(parsed.Groups["year"].Value); var month = Int32.Parse(parsed.Groups["month"].Value); var day = Int32.Parse(parsed.Groups["day"].Value); var hours = Int32.Parse(parsed.Groups["hours"].Value); var mins = Int32.Parse(parsed.Groups["mins"].Value); var secs = Int32.Parse(parsed.Groups["secs"].Value); var suffix = parsed.Groups["suffix"].Value; var extension = parsed.Groups["extension"].Value; DateTime dateCreated; try { dateCreated = new DateTime(year, month, day, hours, mins, secs, DateTimeKind.Local); } catch (ArgumentException e) { // 31st Feb, etc logger.Error($"Failed to parse DateTime for file path {filePath}", e); parsedConflictFileInfo = default(ParsedConflictFileInfo); return false; } // 'suffix' might be a versioner thing (~date-time), or it might be something added by another tool... // Try searching for it, and if that fails go without try { var withSuffix = Path.Combine(directory, prefix + suffix + extension); if (this.filesystemProvider.FileExists(withSuffix)) { parsedConflictFileInfo = new ParsedConflictFileInfo(filePath, withSuffix, dateCreated); return true; } var withoutSuffix = Path.Combine(directory, prefix + extension); if (this.filesystemProvider.FileExists(withoutSuffix)) { parsedConflictFileInfo = new ParsedConflictFileInfo(filePath, withoutSuffix, dateCreated); return true; } } catch (Exception e) { // We're in the path to return false at this point logger.Error(e, $"Failed to look for base file for conflict file {filePath}: {e.Message}"); } parsedConflictFileInfo = default(ParsedConflictFileInfo); return false; }
public bool TryFindBaseFileForConflictFile(string filePath, out ParsedConflictFileInfo parsedConflictFileInfo) { var directory = Path.GetDirectoryName(filePath); var fileName = Path.GetFileName(filePath); var parsed = conflictRegex.Match(fileName); if (!parsed.Success) { parsedConflictFileInfo = default(ParsedConflictFileInfo); return(false); } var prefix = parsed.Groups["prefix"].Value; var year = Int32.Parse(parsed.Groups["year"].Value); var month = Int32.Parse(parsed.Groups["month"].Value); var day = Int32.Parse(parsed.Groups["day"].Value); var hours = Int32.Parse(parsed.Groups["hours"].Value); var mins = Int32.Parse(parsed.Groups["mins"].Value); var secs = Int32.Parse(parsed.Groups["secs"].Value); var device = parsed.Groups["device"].Value; if (string.IsNullOrWhiteSpace(device)) { device = null; } var suffix = parsed.Groups["suffix"].Value; var extension = parsed.Groups["extension"].Value; DateTime dateCreated; try { dateCreated = new DateTime(year, month, day, hours, mins, secs, DateTimeKind.Local); } catch (ArgumentException e) { // 31st Feb, etc logger.Error($"Failed to parse DateTime for file path {filePath}", e); parsedConflictFileInfo = default(ParsedConflictFileInfo); return(false); } // 'suffix' might be a versioner thing (~date-time), or it might be something added by another tool... // Try searching for it, and if that fails go without try { var withSuffix = Path.Combine(directory, prefix + suffix + extension); if (this.filesystemProvider.FileExists(withSuffix)) { parsedConflictFileInfo = new ParsedConflictFileInfo(filePath, withSuffix, dateCreated, device); return(true); } var withoutSuffix = Path.Combine(directory, prefix + extension); if (this.filesystemProvider.FileExists(withoutSuffix)) { parsedConflictFileInfo = new ParsedConflictFileInfo(filePath, withoutSuffix, dateCreated, device); return(true); } } catch (Exception e) { // We're in the path to return false at this point logger.Error(e, $"Failed to look for base file for conflict file {filePath}: {e.Message}"); } parsedConflictFileInfo = default(ParsedConflictFileInfo); return(false); }