public static FoundFile FindText(string searchText, string filePath, string includeFiles, string excludeFiles, bool matchSearchTextCase, bool checkNumberOfLinesBetweenSearchTextEntries, int?linesBetweenSearchText, DateFilterCriteria dateFilterCriteria) { FoundFile foundFile = null; var fileLines = File.ReadAllLines(filePath); FileInfo fileInfo = new FileInfo(filePath); var searchLines = true; if (!string.IsNullOrEmpty(includeFiles)) { searchLines = _DetermineIfMatchesIncludeFilesFilter(includeFiles, fileInfo); } if (searchLines && !string.IsNullOrEmpty(excludeFiles)) { searchLines = _DetermineIfMatchesExcludeFilesFilter(excludeFiles, fileInfo); } if (searchLines) { searchLines = _DetermineIfMatchesDateFilter(fileInfo, dateFilterCriteria); } List <FoundTextLine> foundTextLines = new List <FoundTextLine>(); if (checkNumberOfLinesBetweenSearchTextEntries && linesBetweenSearchText.HasValue) { foundTextLines = _FindTextLinesWithLinesBetweenSearchText(fileLines, searchText, searchLines, matchSearchTextCase, linesBetweenSearchText.Value); } else { foundTextLines = _FindTextLines(fileLines, searchText, searchLines, matchSearchTextCase); } if (foundTextLines.Count > 0) { foundFile = new FoundFile { FileName = fileInfo.Name, FilePath = filePath, FileType = fileInfo.Extension, CreatedOnDate = fileInfo.CreationTime, LastAccessedDate = fileInfo.LastAccessTime, LastModifiedDate = fileInfo.LastWriteTime, FoundTextLines = foundTextLines.ToArray() }; } return(foundFile); }
private void worker_Search(object sender, DoWorkEventArgs e) { var datefilterCriteria = new DateFilterCriteria { CreatedAfterDate = CreatedAfterDate, CreatedBeforeDate = CreatedBeforeDate, LastAccessedAfterDate = LastAccessedAfterDate, LastAccessedBeforeDate = LastAccessedBeforeDate, ModifiedAfterDate = ModifiedAfterDate, ModifiedBeforeDate = ModifiedBeforeDate }; // Have file check to get rid of error if user tries to use fileName as a directory or an invalid directory name if (!File.Exists(SearchPath) && Directory.Exists(SearchPath)) { var searchOption = SearchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; var filePaths = Directory.EnumerateFiles(SearchPath, "*", searchOption); var numberOfFilesFound = 0; foreach (var filePath in filePaths) { if (_backgroundWorker.CancellationPending == true) { e.Cancel = true; return; } var foundFile = FileSearcher.FindText(SearchText, filePath, IncludeFiles, ExcludeFiles, MatchSearchTextCase, CheckNumberOfLinesBetweenSearchTextEntries, LinesBetweenSearchText, datefilterCriteria); if (foundFile != null) { numberOfFilesFound++; (sender as BackgroundWorker).ReportProgress(numberOfFilesFound, foundFile); } } } else { MessageBox.Show("An invalid file path was entered. Please enter a new path", "Invalid file path", MessageBoxButton.OK); } }
private static bool _DetermineIfMatchesDateFilter(FileInfo fileInfo, DateFilterCriteria dateFilterCriteria) { bool matchesDateFilter; matchesDateFilter = _DetermineIfMatchesCreatedDate(fileInfo, dateFilterCriteria.CreatedBeforeDate, dateFilterCriteria.CreatedAfterDate); if (matchesDateFilter) { matchesDateFilter = _DetermineIfMatchesLastAccessedDate(fileInfo, dateFilterCriteria.LastAccessedBeforeDate, dateFilterCriteria.LastAccessedAfterDate); } if (matchesDateFilter) { matchesDateFilter = _DetermineIfMatchesModifiedDate(fileInfo, dateFilterCriteria.ModifiedBeforeDate, dateFilterCriteria.ModifiedAfterDate); } return(matchesDateFilter); }