Search code examples
emeditor

Find in Files - Number per file


In EMEditor, is there a way to get the number of occurrences of a "find in files" search per file? In other words, it finds 10,000 "hits" across 25 files, I'd like to know that 1200 where in file1 etc.
Notepad++ does a great job of this by allowing you to collapse the results by file and showing a summary for each, but I haven't seen a way to get the information in EMEditor.


Solution

  • After Find in Files, you can run this macro while the results document is active. Save this code as, for instance, statistics.jsee, and then select this file from Select... in the Macros menu. Finally, do Find in Files, and select Run in the Macros menu while the results document is active.

    // Creates statistics from Find in Files Results.  
    // 2020-06-27
    Redraw = false;
    sOutput = "";
    y = 1;
    yMax = document.GetLines();
    for( ;; ) {
        document.selection.SetActivePoint( eePosLogical, 1, y++ );
        document.selection.Mode = eeModeStream | eeModeKeyboard;
        bFound = document.selection.Find("\\(\\d+?\\)\\:",eeFindNext | eeFindReplaceCase | eeFindReplaceRegExp,0);
        document.selection.Mode = eeModeStream;
        if( !bFound ) {
            break;
        }
        sFile = document.selection.Text;
        n = sFile.lastIndexOf("(");
        sFile = sFile.substr( 0, n );
        nCount = 1;
        for( ;; ) {
            document.selection.SetActivePoint( eePosLogical, 1, y );
            sLine = document.GetLine( y );
            if( sLine.length > sFile.length && sLine.substr( 0, sFile.length ) == sFile ) {
                ++nCount;
                ++y;
            }
            else {
                sOutput += sFile + "\t" + nCount + "\n";
                break;
            }
        }
    }
    document.selection.Mode = eeModeStream;
    Redraw = true;
    editor.NewFile();
    document.write( sOutput );
    editor.ExecuteCommandByID(4471);  // switch to TSV mode