Tapatalk

Perform FindinFiles using list of strings in an open file

Perform FindinFiles using list of strings in an open file

2
NewbieNewbie
2

    Oct 03, 2012#1

    Version UE18.20

    I have a list of strings on separate lines in a file that I need to loop through and use each individual string in a FindinFiles operation with a user selectable folder searching all the subdirectories and output the results for the entire run in a single new file.

    I've had a look through the Scripts forum entries and haven't seen anything that is similar to this.

    Any help on this greatly appreciated.

    6,685587
    Grand MasterGrand Master
    6,685587

      Oct 03, 2012#2

      That script was very easy to write.

      Code: Select all

      if (UltraEdit.document.length > 0) {   // Is any file currently opened?
      
         // Define environment for the script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.selectAll();
      
         if (UltraEdit.activeDocument.isSel()) {  // Is active file not an empty file?
      
            // Determine line termination.
            var sLineTerm = "\r\n";
            if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
      
            // Get all lines with the search strings into an array of strings.
            var asSearchStrings = UltraEdit.activeDocument.selection.split(sLineTerm);
            UltraEdit.activeDocument.cancelSelect();
      
            // Ask script user for directory path.
            var sDirectory = UltraEdit.getString("Please enter full directory path:",1);
            // If no directory path is entered, use current working directory of UltraEdit.
            // If directory path is entered, make sure it ends with a backslash.
            if (!sDirectory.length) sDirectory = ".\\";
            else if (sDirectory.match(/\\$/) == null) sDirectory += "\\";
            // Set the entered directory as root directory for the finds.
            UltraEdit.frInFiles.directoryStart=sDirectory;
      
            // Define the other parameters for the multiple Find in Files.
            UltraEdit.frInFiles.filesToSearch=0;           // Search in a directory
            UltraEdit.frInFiles.searchSubs=true;           // and all subdirectories.
            UltraEdit.frInFiles.searchInFilesTypes="*.*";  // Search in all files.
            UltraEdit.frInFiles.matchCase=true;            // Run a case sensitive search because faster.
            UltraEdit.frInFiles.matchWord=false;           // Strings not words should be found.
            UltraEdit.frInFiles.useOutputWindow=false;     // Write found lines to a document window.
            UltraEdit.frInFiles.regExp=false;              // Run simple, non regular expression finds.
      
            // Advanced Find in Files options:
            UltraEdit.frInFiles.displayLinesDoNotMatch=false;  // Find lines with string, not opposite.
            UltraEdit.frInFiles.useEncoding=false;         // Use no special encoding, use system code page.
            UltraEdit.frInFiles.ignoreHiddenSubs=false;    // Don't ignore hidden directories, search in all subdirectories.
            UltraEdit.frInFiles.reverseSearch=false;       // We are not interested in files not containing searched string.
            UltraEdit.ueReOn();
      
            // Run now the Find in Files with the strings in a loop.
            for (var nSearchIndex = 0; nSearchIndex < asSearchStrings.length; nSearchIndex++) {
               // Ignore empty lines in list file with the strings to search.
               if (!asSearchStrings[nSearchIndex].length) continue;
               UltraEdit.frInFiles.find(asSearchStrings[nSearchIndex]);
            }
            UltraEdit.activeDocument.top();   // Move caret to top of file with the results.
            // This last command works only if there was not already the results
            // window open on script start as results document window becomes not
            // automatically active if already existing on a Find in Files.
         }
      }
      Please note that the script user cannot select the directory. There is no scripting command which opens a Browse for Directory dialog and returns the full path of the selected directory as string back to the script. The script user must enter the complete directory path or alternatively copy and paste it for example from address bar of Windows Explorer.

      The current settings at Advanced - Configuration - Search - Set Find Output Format define the content of results file after script finished. So you can first configure the output or use some regular expression replaces after the loop to reformat the results file to the output you want.

      Topic Find all lines in active file in all files within a directory contains nearly the same script although I have not used it as template for this script. It looks like similar requirements results really often in similar solutions.

      2
      NewbieNewbie
      2

        Oct 04, 2012#3

        Thanks, Mofi, works a treat.

        V. useful info on output formatting as well.

        Found I needed to look for the exact word match and use a filename wildcard value to focus the search but both easily done for those inexperienced but interested like me :-

        Code: Select all

              UltraEdit.frInFiles.searchInFilesTypes="*M";  // Search in specific fileset of filenames ending in 'M'
              UltraEdit.frInFiles.matchWord=true;           // Match whole word
        Very much appreciated, think I need to get to grips with other scripts in this forum and the Javascript tutorial! :P