How to make multiple replaces within a selection?

How to make multiple replaces within a selection?

18
Basic UserBasic User
18

    Mar 28, 2013#1

    I need some help please. I'm trying to find string "XYZ123" (a delimiting line) in a file. Once I find the string, I note the line it is on, return to the top of the file, and then line, by line go through the file until I'm at the line with "XYZ123" on it and stop. I don't want all occurrences changed. I can't get anything to work. Here is the concept:
    1. Find the string.
    2. Store the line number in a var
    3. Go to the top of the file.
    4. As I go through the file, line by line I would (for instance):

      if("January" is on this line, change it to "01/"
      if("February" is on this line, change it to "02/"
    5. Go until the limit (the line with XYZ123 on it) is reached.
    I've been working on this and other stuff for hours, and I've thrown my hands up on it. I know it is a simple answer, by I've had a brain F*RT.

    Can any one help me PLEASE. Thanks in advance.

    6,603547
    Grand MasterGrand Master
    6,603547

      Mar 28, 2013#2

      This is very simple to achieve. Read the comments in the script below.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.top();
      
         // Define find parameters for first search.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=false;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
         // Find the line containing the string XYZ123.
         if (UltraEdit.activeDocument.findReplace.find("XYZ123"))
         {
            // Move caret to beginning of this line.
            UltraEdit.activeDocument.gotoLine(0,1);
            // Select upwards all lines above to top of file.
            UltraEdit.activeDocument.selectToTop();
      
            // Now this block could be loaded into a string variable and modified
            // in memory by the script to finally write it back to the file. It
            // would be also possible to run replaces on selected block, but that
            // would require reselecting the block again and again after every
            // replace with the selection.
            // But the easiest method is to copy this block to a new file and
            // modify the block there. After finishing all modifications, the
            // block is copied back to original file and the new temporary
            // file is deleted without saving it.
            if (UltraEdit.activeDocument.isSel())
            {
               UltraEdit.selectClipboard(9);   // Use user clipboard 9.
               UltraEdit.activeDocument.copy();
               // Remember document index of active file.
               var nActiveFile = UltraEdit.activeDocumentIdx;
               UltraEdit.newFile();
               // Paste the copied block and move caret to top of file.
               UltraEdit.activeDocument.paste();
               UltraEdit.activeDocument.top();
               // Define a variable which remembers if block is modified at all.
               var bBlockModified = false;
      
               // Define all the parameters for the replaces in temporary file.
               UltraEdit.activeDocument.findReplace.mode=0;
               UltraEdit.activeDocument.findReplace.matchCase=true;
               UltraEdit.activeDocument.findReplace.matchWord=false;
               UltraEdit.activeDocument.findReplace.regExp=false;
               UltraEdit.activeDocument.findReplace.searchDown=true;
               UltraEdit.activeDocument.findReplace.searchInColumn=false;
               UltraEdit.activeDocument.findReplace.preserveCase=false;
               UltraEdit.activeDocument.findReplace.replaceAll=true;
               UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
               // Run now several case-sensitive, non regular expression Replace All
               // from top of temporary file. Caret position does not change here.
               if (UltraEdit.activeDocument.findReplace.replace("January","01/"))  bBlockModified = true;
               if (UltraEdit.activeDocument.findReplace.replace("February","02/")) bBlockModified = true;
               // And so on.
      
               if (bBlockModified)  // Is the block modified at all?
               {
                  UltraEdit.activeDocument.selectAll();
                  UltraEdit.activeDocument.copy();
                  UltraEdit.document[nActiveFile].paste();
               }
               UltraEdit.document[nActiveFile].top();
               UltraEdit.clearClipboard();
               UltraEdit.selectClipboard(0);
               UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
               UltraEdit.document[nActiveFile].setActive();
            }
         }
      }

      18
      Basic UserBasic User
      18

        Mar 28, 2013#3

        Mofi wrote:This is very simple to achieve. Read the comments in the script below.
        Two things.
        1. First THANKS!
        2. It was more involved than **I thought** it would be. :oops: