Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / WPF

WPF File Search Tool

Rate me:
Please Sign up or sign in to vote.
2.12/5 (6 votes)
13 May 2017CPOL3 min read 11.5K   638   6  
A file search utility with a few extra tricks for anyone who needs to search inside files other than those known by windows explorer.

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

The Search Tab:

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.

The Capture Tab:

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()

C#
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:

C#
                        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Jacobs Technology
United States United States
Rick is a Senior Software Engineer with 20+ years of software engineering experience and is currently working for Jacobs Technology on the IRES Contract at Colorado Springs, CO.

He holds degrees in:
AAS Electronics Systems Technology
AS Computer Science
BS Computer Science
MS Computer Science

Comments and Discussions

 
-- There are no messages in this forum --