finding a string and create a report

finding a string and create a report

10
Basic UserBasic User
10

    Jun 19, 2007#1

    Hi,

    I am just learning JavaScript with UltraEdit and had a few questions. This is what I would like to accomplish with the script:

    1. Have a predefined list of strings.
    2. Tell JavaScript to find those strings in the document.
    3. Create a "report" in the output widow (or second best, in a new file) telling which line(s) each word appears on.

    Is this even possible with JavaScript in UE? After reading some of the other posts I am getting the feeling that UE is somewhat limited in what we can do...

    Any help would be appreciated! :)

    Thanks,

    -Andy

    262
    MasterMaster
    262

      Jun 19, 2007#2

      Sure it will be possible. As long as we are talking core JavaScript there are no notable limitations in UltraEdit.

      Have a look at the resources in the sticky of this forum subcategory. There you'll find a lot of inspiration to what you are working on.

      Some hints for you:
      1. You'll need an array...
      2. You'll want to use UltraEdit.activeDocument.findReplace.find... and UltraEdit.activeDocument.selection
      3. Write to the outputWindow...

      Have fun learning !

      10
      Basic UserBasic User
      10

        Jun 20, 2007#3

        Thanks for the help!

        Here is a start:


        var x
        var myString = new Array()
        myString[0] = /Hi/g
        myString[1] = /Hello/g

        for (x in myString)
        {
        UltraEdit.activeDocument.findReplace.find(myString[x]);
        }
        UltraEdit.activeDocument.top();
        UltraEdit.activeDocument.selectWord();
        var line = UltraEdit.activeDocument.selection;
        UltraEdit.outputWindow.write(line)


        How would I go about getting the line number and make it loop through the whole document?

        TIA for your help!


        BTW it seems like this code should work, but it doesn't:


        UltraEdit.activeDocument.top();
        UltraEdit.activeDocument.selectLine(1,1);
        UltraEdit.activeDocument.findReplace.matchWord = true;
        UltraEdit.activeDocument.findReplace.find("hello");
        UltraEdit.activeDocument.selectLine();
        var lineNum = UltraEdit.activeDocument.currentLineNum;
        UltraEdit.outputWindow.write(lineNum);


        What am I doing wrong?

        Thanks,

        -al

        6,602548
        Grand MasterGrand Master
        6,602548

          Jun 21, 2007#4

          Look on power tip UltraEdit/UEStudio Scripting Access to the Output Window which contains an example which is very similar to what you want to do.
          Best regards from an UC/UE/UES for Windows user from Austria

          10
          Basic UserBasic User
          10

            Jun 21, 2007#5

            WOW!!! Thanks Mofi!!! That is very helpful!

            Now all I need is an array and it should work! :D

              Jun 22, 2007#6

              Code: Select all

              //This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
              
              //Get user input
              var findStr;
              var x = 0;
              var lineNum;
              
              var y
              var myStrings = new Array()
              myStrings[0] = "hi"
              myStrings[1] = "hello"
              
              //Clear the output window, make it visible and disable status information.
              UltraEdit.outputWindow.showStatus=false;
              UltraEdit.outputWindow.clear();
              if (UltraEdit.outputWindow.visible == false) {
                UltraEdit.outputWindow.showWindow(true);
              }
              
              for (y in myStrings)
              {
              findStr = myStrings[y];
              //Make sure we start at the beginning of the file for all values
              UltraEdit.activeDocument.top();
              myFindFunc(findStr);
              }
              
              function myFindFunc()
              {
              UltraEdit.outputWindow.write("");
              UltraEdit.outputWindow.write("--- Search String ---");
              UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
              UltraEdit.outputWindow.write("");
              
              UltraEdit.outputWindow.write("--- Line Numbers ---");
              UltraEdit.activeDocument.findReplace.find(findStr);
              //loop to end of file
              while (!(UltraEdit.activeDocument.isEof())) {
                if (UltraEdit.activeDocument.isFound()) {
                  //get the line number that findStr is found on
                  lineNum = UltraEdit.activeDocument.currentLineNum;
                  UltraEdit.outputWindow.write("Found \"" + findStr + "\" on line: " + lineNum);
                  //increment count
                  ++x;
                  } 
                  else 
                  {
                  UltraEdit.activeDocument.bottom();
                  break;
                  }
                UltraEdit.activeDocument.findReplace.find(findStr);
              }
              
              
              UltraEdit.outputWindow.write("");
              UltraEdit.outputWindow.write("--- Total Orders ---");
              UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
              UltraEdit.outputWindow.write("");
              x = 0
              }
              
              There... thanks for the help! I get a "report" of the following:

              --- Search String ---
              You searched for "hi"

              --- Line Numbers ---
              Found "hi" on line: 1
              Found "hi" on line: 2

              --- Total Orders ---
              Total for "hi" is: 2


              --- Search String ---
              You searched for "hello"

              --- Line Numbers ---
              Found "hello" on line: 9
              Found "hello" on line: 11

              --- Total Orders ---
              Total for "hello" is: 2

              Now what I would like to do is make it so when I click on "Found "hello" on line: 11" it will actually go to line 11.

              Any suggestions?

              Thanks,

              -al

              262
              MasterMaster
              262

                Jun 22, 2007#7

                With this in mind:
                UltraEdit Help wrote:Output Window command (View menu)
                ...
                Double clicking on a line containing the filename and line number will cause UltraEdit to attempt to open the file specified at the line number. If the filename is fully qualified the filename will be located and the number after this will be used for the line number. If the filename is not fully qualified UltraEdit will attempt to determine the filename from the first word in the line that contains a period. UltraEdit will then open the specified file in the directory of the active file. If the file does not exist in the directory of the active file, UltraEdit will try and open the file from the Project Directory if it is specified.
                you could write something like:

                Code: Select all

                UltraEdit.outputWindow.write("Found "" + findStr + "" in file "+UltraEdit.activeDocument.path+" on line: " + lineNum);
                resulting in something like:
                Found "Hello" in file C:\Programmer\IDM Computer Solutions\scripts\temp.js on line: 4

                then if you double click this you will be positioned to that line.

                A shorter version of the filename could be achived by this:

                Code: Select all

                // extract filename from path using regexp and match method.
                var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
                UltraEdit.outputWindow.write("Found "" + findStr + "" in file "+fileName+" on line: " + lineNum);
                
                (this will work for windows path with backslashes)

                resulting in:
                Found "Hello" in file alignEqual-v13.10.js on line: 4

                10
                Basic UserBasic User
                10

                  Jun 22, 2007#8

                  Thanks Jorrasdk!!!! You guys are awesome!!!! :D

                    Jun 24, 2007#9

                    Code: Select all

                    //This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
                    
                    var findStr;
                    var x = 0;
                    var lineNum;
                    
                    var y
                    var myStrings = new Array()
                    myStrings[0] ="hi"
                    myStrings[1] ="hello"
                    
                    //Clear the output window, make it visible and disable status information.
                    UltraEdit.outputWindow.showStatus=false;
                    UltraEdit.outputWindow.clear();
                    if (UltraEdit.outputWindow.visible == false) {
                      UltraEdit.outputWindow.showWindow(true);
                    }
                    
                    for (y in myStrings)
                    {
                    findStr = myStrings[y];
                    UltraEdit.activeDocument.top();
                    myFindFunc(findStr);
                    }
                    
                    function myFindFunc()
                    {
                    UltraEdit.outputWindow.write("");
                    UltraEdit.outputWindow.write("--- Search String ---");
                    UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
                    UltraEdit.outputWindow.write("");
                    
                    UltraEdit.outputWindow.write("--- Line Numbers ---");
                    UltraEdit.activeDocument.findReplace.find(findStr);
                    //loop to end of file
                    while (!(UltraEdit.activeDocument.isEof())) {
                      if (UltraEdit.activeDocument.isFound()) {
                        //get the line number that findStr is found on
                        lineNum = UltraEdit.activeDocument.currentLineNum;
                        var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
                        UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
                        //increment count
                        ++x;
                        } 
                        else 
                        {
                        UltraEdit.activeDocument.bottom();
                        break;
                        }
                      UltraEdit.activeDocument.findReplace.find(findStr);
                    }
                    
                    
                    UltraEdit.outputWindow.write("");
                    UltraEdit.outputWindow.write("--- Total Orders ---");
                    UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
                    UltraEdit.outputWindow.write("");
                    x = 0
                    }
                    
                    The last thing I would like to do is make the array reside in another file somewhere (probably in the same folder as the script). Each line in this file would represent one string that I would want the script to search for. So at the end of the day we could just add, remove, or change the strings in that file and not even touch the script file.

                    Any idea how this would work?

                    Thanks,

                    -al

                    262
                    MasterMaster
                    262

                      Jun 24, 2007#10

                      Hi

                      I have added a function myGetParms() and a support function getActiveDocumentIndex():

                      Code: Select all

                      //This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
                      
                      var findStr;
                      var x = 0;
                      var lineNum;
                      var y;
                      
                      //Clear the output window, make it visible and disable status information.
                      UltraEdit.outputWindow.showStatus=false;
                      UltraEdit.outputWindow.clear();
                      if (UltraEdit.outputWindow.visible === false) {
                        UltraEdit.outputWindow.showWindow(true);
                      }
                      
                      var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
                      var myStrings = myGetParms(); /* get parms from external file - returned as array */
                      UltraEdit.document[workDocIx].setActive(); /* restore active document */
                      
                      for (y in myStrings)
                      {
                      findStr = myStrings[y];
                      UltraEdit.activeDocument.top();
                      myFindFunc(findStr);
                      }
                      
                      function myFindFunc(findStr)
                      {
                      UltraEdit.outputWindow.write("");
                      UltraEdit.outputWindow.write("--- Search String ---");
                      UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
                      UltraEdit.outputWindow.write("");
                      
                      UltraEdit.outputWindow.write("--- Line Numbers ---");
                      UltraEdit.activeDocument.findReplace.find(findStr);
                      //loop to end of file
                      while (!(UltraEdit.activeDocument.isEof())) {
                        if (UltraEdit.activeDocument.isFound()) {
                          //get the line number that findStr is found on
                          lineNum = UltraEdit.activeDocument.currentLineNum;
                          var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
                          UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
                          //increment count
                          ++x;
                          }
                          else
                          {
                          UltraEdit.activeDocument.bottom();
                          break;
                          }
                        UltraEdit.activeDocument.findReplace.find(findStr);
                      }
                      
                      
                      UltraEdit.outputWindow.write("");
                      UltraEdit.outputWindow.write("--- Total Orders ---");
                      UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
                      UltraEdit.outputWindow.write("");
                      x = 0;
                      }
                      
                      /* Get parameters (search strings) from external file. Lines in external file starting with
                        # in column 1 are considered comment lines. Returns an array.
                      */
                      function myGetParms() {
                      	var parms = new Array();
                      
                      	UltraEdit.open("C:\\Program Files\\IDM Computer Solutions\\scripts\\parms.txt"); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
                      	
                      	UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
                      	UltraEdit.activeDocument.findReplace.regExp = true;
                      
                      	// Just in case: Go to top of document
                      	UltraEdit.activeDocument.top();
                      
                      	while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
                      		parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
                      	}
                      
                      	UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
                      	return parms; /* return array with parameters */
                      }
                      
                      /* Find the tab index of the active document */
                      function getActiveDocumentIndex() {
                         var tabindex = -1; /* start value */
                      
                         for (i = 0; i < UltraEdit.document.length; i++)
                         {
                            if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
                               tabindex = i;
                               break;
                            }
                         }
                         return tabindex;
                      }
                      
                      Look for UltraEdit.open in myGetParms() and adjust to your own parameter file.
                      Each line in the parameter file is a search item. Lines starting with # are considered comments - example:

                      Code: Select all

                      # Put items to search for on a seperate line. # = comments
                      hi
                      #foo
                      hello
                      What do you think ?

                      10
                      Basic UserBasic User
                      10

                        Jun 25, 2007#11

                        Code: Select all

                        //This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
                        
                        var findStr;
                        var x = 0;
                        var lineNum;
                        var y;
                        
                        //Clear the output window, make it visible and disable status information.
                        UltraEdit.outputWindow.showStatus=false;
                        UltraEdit.outputWindow.clear();
                        if (UltraEdit.outputWindow.visible === false) {
                          UltraEdit.outputWindow.showWindow(true);
                        }
                        
                        var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
                        var myStrings = myGetParms(); /* get parms from external file - returned as array */
                        UltraEdit.document[workDocIx].setActive(); /* restore active document */
                        
                        for (y in myStrings)
                        {
                        findStr = myStrings[y];
                        UltraEdit.activeDocument.top();
                        myFindFunc(findStr);
                        }
                        
                        function myFindFunc(findStr)
                        {
                        UltraEdit.outputWindow.write("");
                        UltraEdit.outputWindow.write("--- Search String ---");
                        UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
                        UltraEdit.outputWindow.write("");
                        
                        UltraEdit.outputWindow.write("--- Line Numbers ---");
                        UltraEdit.activeDocument.findReplace.find(findStr);
                        //loop to end of file
                        while (!(UltraEdit.activeDocument.isEof())) {
                          if (UltraEdit.activeDocument.isFound()) {
                            //get the line number that findStr is found on
                            lineNum = UltraEdit.activeDocument.currentLineNum;
                            var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
                            UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
                            //increment count
                            ++x;
                            }
                            else
                            {
                            UltraEdit.activeDocument.bottom();
                            break;
                            }
                          UltraEdit.activeDocument.findReplace.find(findStr);
                        }
                        
                        
                        UltraEdit.outputWindow.write("");
                        UltraEdit.outputWindow.write("--- Total Orders ---");
                        UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
                        UltraEdit.outputWindow.write("");
                        x = 0;
                        }
                        
                        /* Get parameters (search strings) from external file. Lines in external file starting with
                          # in column 1 are considered comment lines. Returns an array.
                        */
                        function myGetParms() {
                           var parms = new Array();
                        
                           UltraEdit.open("C:\\Documents and Settings\\Andrew Larsen.ANDREW-LAPTOP\\Desktop\\javascript_in_UE\\Strings.txt"); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
                           
                           UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
                           UltraEdit.activeDocument.findReplace.regExp = true;
                        
                           // Just in case: Go to top of document
                           UltraEdit.activeDocument.top();
                        
                           while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
                              parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
                           }
                        
                           UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
                           return parms; /* return array with parameters */
                        }
                        
                        /* Find the tab index of the active document */
                        function getActiveDocumentIndex() {
                           var tabindex = -1; /* start value */
                        
                           for (i = 0; i < UltraEdit.document.length; i++)
                           {
                              if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
                                 tabindex = i;
                                 break;
                              }
                           }
                           return tabindex;
                        }
                        
                        Yes this is great, but it has one little problem. It doesn't loop through the whole file. It always takes the last string out, so if I put 3 strings in it will only find the first two. How would I fix this?

                        Thanks,

                        -al

                        344
                        MasterMaster
                        344

                          Jun 25, 2007#12

                          didn't test, but try a CRLF after the last line in the data file.
                          (an empty line at the end).
                          Quite sure it fixes it.

                          rds Bego
                          Normally using all newest english version incl. each hotfix. Win 10 64 bit

                          10
                          Basic UserBasic User
                          10

                            Jun 25, 2007#13

                            Yes... that worked! Thank you guys very much. Your help is very much appreciated!

                            I couldn't have done it without your help!!!!!!!

                            Thanks,

                            -al

                              Jul 12, 2007#14

                              Hi All,

                              I was wondering if the following was possible...

                              First is there any way to make it so I don't have to have that extra blank line at the end of the file in the strings file (the file that has the strings we are looking for)? Could I do it by changing the reg. exp., or I was thinking about adding some code to make it go to the end of the file and add a "enter". Is this possible?

                              Also, I was wondering if there was some sort of "include" or link tag to reference other javascript files?

                              Here is the code I have:


                              [code:]
                              //This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.


                              var findStr;
                              var x = 0;
                              var lineNum;
                              var y;

                              //Clear the output window, make it visible and disable status information.
                              UltraEdit.outputWindow.showStatus=false;
                              UltraEdit.outputWindow.clear();
                              if (UltraEdit.outputWindow.visible === false) {
                              UltraEdit.outputWindow.showWindow(true);
                              }

                              var strFolder = "C:\\Documents and Settings\\Andrew Larsen.ANDREW-LAPTOP\\Desktop\\Javascript_In_UltraEdit\\Strings.txt"
                              var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
                              var myStrings = myGetParms(); /* get parms from external file - returned as array */
                              UltraEdit.document[workDocIx].setActive(); /* restore active document */

                              for (y in myStrings)
                              {
                              findStr = myStrings[y];
                              UltraEdit.activeDocument.top();
                              myFindFunc(findStr);
                              }

                              function myFindFunc(findStr)
                              {
                              UltraEdit.outputWindow.write("");
                              UltraEdit.outputWindow.write("--- Search String ---");
                              UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
                              UltraEdit.outputWindow.write("");

                              UltraEdit.outputWindow.write("--- Line Numbers ---");
                              UltraEdit.activeDocument.findReplace.find(findStr);
                              //loop to end of file
                              while (!(UltraEdit.activeDocument.isEof())) {
                              if (UltraEdit.activeDocument.isFound()) {
                              //get the line number that findStr is found on
                              lineNum = UltraEdit.activeDocument.currentLineNum;
                              var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
                              UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
                              //increment count
                              ++x;
                              }
                              else
                              {
                              UltraEdit.activeDocument.bottom();
                              break;
                              }
                              UltraEdit.activeDocument.findReplace.find(findStr);
                              }


                              UltraEdit.outputWindow.write("");
                              UltraEdit.outputWindow.write("--- Total Orders ---");
                              UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
                              UltraEdit.outputWindow.write("");
                              x = 0;
                              }

                              /* Get parameters (search strings) from external file. Lines in external file starting with
                              # in column 1 are considered comment lines. Returns an array.
                              */
                              function myGetParms() {
                              var parms = new Array();

                              UltraEdit.open(strFolder); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
                              UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
                              UltraEdit.activeDocument.findReplace.regExp = true;

                              // Just in case: Go to top of document
                              UltraEdit.activeDocument.top();

                              while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
                              parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
                              }

                              UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
                              return parms; /* return array with parameters */
                              }

                              /* Find the tab index of the active document */
                              function getActiveDocumentIndex() {
                              var tabindex = -1; /* start value */

                              for (i = 0; i < UltraEdit.document.length; i++)
                              {
                              if (UltraEdit.activeDocument.path==UltraEdit.document.path) {
                              tabindex = i;
                              break;
                              }
                              }
                              return tabindex;
                              }
                              [/code]

                              262
                              MasterMaster
                              262

                                Jul 12, 2007#15

                                andy wrote:First is there any way to make it so I don't have to have that extra blank line at the end of the file in the strings file (the file that has the strings we are looking for)? Could I do it by changing the reg. exp., or I was thinking about adding some code to make it go to the end of the file and add a "enter". Is this possible?
                                This is a classic. A line should only be considered a line if it ends with line terminators (i.e. \r\n for DOS). Otherwise it is a string. (I think I have this quote from Mofi ;-) Therefore as Bego pointed out we had to add line terminators to the last "line" so the "end of line anchor" in the regex ($) still works for the last line.

                                But if you really dislike the last line ending with line terminators, then use this regex instead:
                                %[~#]?+
                                as it doesn't rely on "end of line anchor" and ? matches everything but newline.

                                andy wrote:Also, I was wondering if there was some sort of "include" or link tag to reference other javascript files?
                                Short answer: No, not at the moment.

                                Read more posts (1 remaining)