WPF File Search Tool

0
69

Introduction

The Windows Explorer search features are limited to searching in files with known file types. This tool allows one to search inside any type of file for matching a text pattern. It includes a feature to capture data from the file and insert it into a new text file.

Background

The application uses multiple threads so that the UI is responsive during the search operation. The emphysis here is on the utility and user-friendliness of the application as well as the algorithms and techniques used to do the search.

Using the Application

File Search

Build the application and either run it from the IDE or copy the exe and .config files to your preferred location.

Start the application and select to search a specific “File” or a “Path”.

Checking the “Recurse” option in “Path” mode will cause the tool to search all sub-directores.

Enter any file extensions you want to exclude from the search. This is helpful if you’re searching in directories that contain very large files that you know don’t have what you’re looking for.

Enter a search FilePath or Directory Path or use the elipsis button to open a browse dialog.

Enter the search text you wish to look for. Check the Regular Expression box if you want to use a regex phrase for matching text.

Click the Start button to begin the search. The application will look through any type of file for matching text and list each match in the data grid. Matching file names and paths will also be listed but there won’t be a line number or content to display.

The application includes a number of user settings to provide persistence between sessions. These settings are saved each time the “Start” button is clicked and upon closing the application.

Capture

The capture tab allows the user to grab parts of the line containing the matching text and put it into a new text file. Several options allow for formatting the new file to some extent. I’ve used this for grabbing error messages from a log and creating sql scripts for running against a database or creating a .bat file to do some work elsewhere.

Check the Enable Data Capture box to enable capturing data from the matching files. This feature only operates on text found inside files. Path and FileName matches will not be included in the output.

Enter an output file path or use the elipisis button to specify a save location and name.

Enter the start text to look for in the matching line.

Enter the end text to look for in the matching line.

Check the options desired for the capture feature. They should be self-explanitory for most people. The text box at the bottom is for the “Add Text After Each Output” option.

Points of Interest

Some notable features of the code include the use of threading to keep the UI responsive during a search operation. While there are several different ways to achieve this I opted for the simple Thread.Start()

var t = new Thread(SearchPath);
t.Start(startPath);

Dispatcher.BiginInvoke() was used with an Action object and generic method expression to make UI changes on the UI thread:

                        Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        pbProgressFiles.Value = 0;
                                        pbProgressFiles.Minimum = 0;
                                        pbProgressFiles.Maximum = fileCount;
                                    }));

Using Thread.Yield() allows the UI to update in between line searches.

I’ve included some status updates in-between various processing blocks to let the user know what’s happening while work is being done.

try/catch is also implemented in appropriate places to avoid crashes, notify the user that something went wrong and allow the application to return to a useable state after an error is encountered.

When in “Path” mode the application uses a recursive method, SearchPath(), to search the directory tree starting at the root location provided by the user.

History

n/a

LEAVE A REPLY