Getting find in files to stop on first match



  • Is there a way to make Notepad++ to stop searching after having found its first match?
    I have a folder with thousands of files and it takes a really long time for the tool to search through all the files and
    unfortunately the search result isn’t displayed until the complete search has finished.



  • @Ted-Lundqvist

    The direct answer to your question is (as far as I know) NO.

    However, I have concerns about your use of the term “first” match – in such a search, what does “first” mean? Unless you are an expert on exactly how N++ goes about searching multiple files (and directories), and you know that the first match it finds is the “first” one you are seeking. The way you are currently doing it, how do you know that the first match doesn’t occur until N++ examines the very last file that it possibly can?–in which case there really is no wasted time.

    My thinking is that you want to know if ANY match occurs, and stop the search on that event.

    A possible workaround to your problem would be to drop into a CMD shell (right-click the editor window tab for a file at the top level of the directory tree to search, choose “Open Containing Folder in cmd”), and use the FINDSTR command; here’s an example:

    FINDSTR /S /N /C:yourSearchTextHere *.txt

    Then let it run and you can kill it with CTRL+C once it finds a match and outputs it to the screen. Thus, no additional waiting when “any” match is found.

    Note: you didn’t mention a folder tree in your question, but I included it in the answer because it is a more general case of the stated problem.

    If it is more important to you to have this capability native in Notepad++ than to actually achieve the goal, then my workaround suggestion is pointless and your question really turns into a feature request.   :-)



  • @Ted-Lundqvist You might also do a web search for GrepWrap. It is a multi-threaded windows app that allows you to scroll through the results window and open files if you want while it continues to search. It also allows you to exclude files and directories from the search using regular expressions.



  • A Pythonscript could also be written to perform the “stop-on-any-match” functionality…to keep it “within” Notepad++…



  • Here’s an example of such a Pythonscript:

    import os
    import fnmatch
    search_string              = 'import'
    top_level_dir              = r'...\npp.bin\plugins\Config\PythonScript\scripts'
    filespecs_in_space_sep_str = '*.py *.txt'
    files_opened_count = 0
    try:
        for (root, dirs, files) in os.walk(top_level_dir):
            for file in files:
                for filespec in filespecs_in_space_sep_str.split():
                    if fnmatch.fnmatch(file, filespec):
                        full_path = os.path.join(root, file)
                        with open(full_path) as f:
                            files_opened_count += 1
                            for (line_nbr, line_content) in enumerate(f):
                                if search_string in line_content:
                                    notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, 'match found at line {0} in file "{1}"'.format(line_nbr + 1, full_path))
                                    raise StopIteration  # stop after ANY match
                            # assuming files are big and match hasn't been found; give user some feedback on what is going on:
                            notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, '# files checked: {}'.format(files_opened_count))
    except StopIteration:
        pass
    else:
        notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, 'NO MATCH [# files checked: {}]'.format(files_opened_count))


  • Thank you for your excellent support!
    Your suggestions definitely solves my problem and there is no need to have this feature implemented in N++.


Log in to reply